【问题标题】:How to add a new language string in WordPress?如何在 WordPress 中添加新的语言字符串?
【发布时间】:2015-03-05 17:25:23
【问题描述】:

基本上我想把本站“Previous Work”和“Next work”的“Previous post”和“Next Post”这两个词改成:http://fontaneriaborja.com/en/portfolio-view/solar-panels-swimming-pool/

我已经进入 /wp-content/themes/CherryFramework/loop/loop-single-portfolio.php 并找到了这段代码:

<!--BEGIN .pager .single-pager -->
        <ul class="pager single-pager">
        <?php if (get_previous_post()) : ?>
            <li class="previous"><?php previous_post_link('%link', theme_locals("prev_post")) ?>
        <?php endif; ?>

        <?php if (get_next_post()) : ?>
            <li class="next"><?php next_post_link('%link', theme_locals("next_post")) ?>
        <?php endif; ?>
<!--END .pager .single-pager -->

我相信 theme_locals("prev_post") 和 theme_locals("next_post") 是调用文本的字符串。

理想情况下,我想做的是为诸如 theme_locals("prev_work") 和 theme_locals("next_work") 之类的内容更改和添加一个新字符串,这将为我的 .po 文件生成两个新的翻译。但是,这是我卡住的地方。

这是一个多语言 WordPress 4.1.1 网站,使用 Polylang 和 Template Monster 的主题和樱桃插件。

我已经四处搜索,但没有运气了解它是如何工作的。因此,我们将不胜感激。

感谢您抽出宝贵时间,并提前感谢大家。

最好的

P.S.:我是 Web 开发方面的初学者。 P.S.2:我也在https://wordpress.org/support/topic/add-language-string-in-wordpress 上发布了这个,如果我找到解决方案,我会尽量保持更新。

后编辑:

正如danbahrami 所建议的,我在/wp-content/themes/CherryFramework/includes/locals.php 中找到了一个带有函数'theme_local_init()' 的php 文件。这是部分代码,因为太长了,我没有全部放出来:

<?php
function theme_local_init() {
    global $is_cherry_local_init, $cherry_locals_arr;
    if ($is_cherry_local_init) return true;

    $domain = CURRENT_THEME;
    $cherry_locals_arr = array(
        //general
        'no' => __('No', 'cherry'),
        'yes' => __('Yes', $domain),
        'slow_speed' => __('Slow', $domain),
        'normal_speed' => __('Normal', $domain),
        'fast_speed' => __('Fast', $domain),
        'normal_size' => __('Normal size', $domain),
        'large_size' => __('Large size', $domain),
        'font_size' => __('Font Size', $domain),
        'lineheight' => __('Lineheight', $domain),
        'font_face' => __('Font Face', $domain),
        'character_sets' => __('Character Sets', $domain),
        'font_style' => __('Font Style', $domain),
        'color' => __('Color', $domain),
        'import' => __('Import', $domain),
        'export' => __('Export', $domain),
        'done' => __('Done', $domain),
        'error' => __('Error', $domain),
        'success' => __('success', $domain),
        'upload' => __('Upload', $domain),
        'try_again' => __('try again', $domain),
        'finish' => __('Finish', $domain),
        'skip' => __('Skip this step', $domain),
        'install_next' => __('next', $domain),
        'none' => __('None', $domain),
        'date' => __('Date', $domain),
        'title' => __('Title', $domain),
        'info' => __('Info', $domain),
        'rand' => __('Random', $domain),
        'comment_count' => __('Comment count', $domain),
        'enable_lightbox' => __('Enable Lightbox', $domain),
        'enable_lightbox_desc' => __('Check this to enable the lightbox.', $domain),
        'permalink_to' => __('Permalink', $domain),
        'read_more' => __('Read more', $domain),
        'view_all' => __('View all', $domain),
        'width' => __('Width', $domain),
        'height' => __('Height', $domain),
        'excerpt_length' => __('Excerpt length (words):', $domain),
        'link_text' => __('Link Text:', $domain),
        'link_url' => __('Link URL', $domain),
        'standard' => __('Standard', $domain),
        'aside' => __('Aside', $domain),
        'quote' => __('Quote', $domain),
        'link' => __('Link', $domain),
        'image' => __('Image', $domain),
        'gallery' => __('Gallery', $domain),
        'audio' => __('Audio', $domain),
        'video' => __('Video', $domain),
        'categories' => __('Categories', $domain),
        'tags' => __('Tags', $domain),
        'show_all' => __('Show All', $domain),
        'search' => __('search', $domain),
        'go' => __('Go', $domain),
        'prev_post' => __('&laquo; Previous post', $domain),
        'next_post' => __('Next Post &raquo;', $domain),

我相信最后两行是有问题的,但还有更多代码。

【问题讨论】:

  • “theme_locals()”函数正在返回文本。如果你浏览你的主题文件,你会发现在某处定义的函数......可能在名为 functions.php 的文件中。如果你不能并且有一个好的代码编辑器,那么应​​该有一个工具来搜索所有文件。尝试搜索“function theme_locals”并将以下 {} 括号之间的所有内容粘贴到您的问题中,这将非常有帮助。
  • 我想我已经添加了你需要的东西。希望这会有所帮助,谢谢。

标签: php string wordpress translation language-translation


【解决方案1】:

您需要做的就是将theme_local_init 函数的最后两行从...更改为...

'prev_post' => __('&laquo; Previous post', $domain),
'next_post' => __('Next Post &raquo;', $domain),

到...

'prev_post' => __('&laquo; Previous work', $domain),
'next_post' => __('Next work &raquo;', $domain),

如果您有兴趣了解有关 wordpress 如何处理翻译的更多信息,您应该阅读The i18n page in the Wordpress codex。它可以帮助您了解您的主题是如何工作的。

更新: 或者如果您只想在某些情况下更改文本,您可以在函数中添加更多行,例如

'prev_work' => __('&laquo; Previous work', $domain),
'next_work' => __('Next work &raquo;', $domain),

然后将模板中的post_link 函数更改为使用prev_worknext_work

然后在您下次生成 PO 文件时,将添加新的可翻译字符串。

希望有帮助

【讨论】:

  • 嗨,Dan,这意味着它将在模板上的任何地方更改该短语。我只想在这种特殊情况下更改这些词。如果我添加两行,例如 'prev_work' => __('« Previous work', $domain), 'next_work' => __('Next work »', $domain), 会起作用吗?但是,它会在 .po 文件中生成这些行吗?这是我不知道如何进行的地方
  • 我已经更新了我的答案来解决这个问题......基本上是的,你是正确的,当你生成你的 PO 文件时,新的字符串将被包含在内。
  • 感谢 Dan,为现在展示 Next Work 和 Previous Work 所做的努力。但是,我下载了 PO 文件,但找不到新字符串。我需要做些什么来添加它吗?我什至在发布之前访问了 Wordpress codex 链接,但我很难理解。
  • 翻译没那么简单,wordpress 不会自动为你生成 .po 文件。您需要下载一个名为 poedit 的软件包并自己重新生成 .po 文件。
  • 我知道 Poedit 并用它打开文件。但是 .po 文件不包含 '« 的字符串。以前的工作”或“下一个工作”。我用 Sublime 文本打开,我也看不到那里的字符串我需要手动将任何代码包含到 .po 文件中吗?顺便说一句,感谢您的所有帮助
猜你喜欢
  • 2011-09-25
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多