dousi6405 2017-07-21 13:48
浏览 184

Polylang Wordpress插件自定义字符串翻译无法按预期方式工作

I'm using Polylang plugin on recent website. Posts and pages, also categories translations work perfectly. But I have problem with translations of custom strings.

I am registering string in my functions.php file with:

pll_register_string('read_more', 'Read More');

Then, in my template view I'm trying to get access to it by:

pll_e('read_more');

In response, I'm getting the "read_more"... The same if I tried to:

pll_e('Read more');

According to function reference of that plugin, it's a proper way. Maybe someone had a similiar problem in the past and can help.

Edit: I've conquer the problem by using Polylang extension for custom string. But, maybe someone can resolve this anyway.

  • 写回答

2条回答 默认 最新

  • douji4223 2017-11-13 11:58
    关注

    I am not sure if it helps you perfectly but the best approach I am using in this case is to create .po and .mo file for each language, upload it to a website and register it in functions.php or in plugin with this function (you must set path where .po and .mo files will be stored - bellow it's plugin path but you can change it to whatever you want and also you can change "my-theme" tag to your own):

    // Adding language files
    function theme_setup_hook () {
     load_theme_textdomain('my-theme', plugin_dir_path(__FILE__).'/languages');
    }
    add_action( 'after_setup_theme', 'theme_setup_hook' );
    

    In .po file using Poedit (lightweight open source app which can be downloaded here https://poedit.net/) you just set the language of the file in Properties and select the folder on your disc where files with code with strings to translate are written. Also you must set Keywords in properties where I put these: __ and _e.

    In the template's code you then write all strings which needs to be translated like this:

    for simple echo:

    <?php _e('string to translate','my-theme'); ?>
    

    for using in scripts:

    <?php
     $string = __('string to translate','my-theme');
     echo $string;
    ?>
    

    After that just Update the po file in Poedit and since you have set proper path all strings are updated here and you can all translate them. Then save the file. Poedit automatically create also .mo file so upload both of files (po and mo) back to server and that's it. For each language you can do that with separate file which is named like en_GB.po etc.

    I hope it helps.

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题