使用 Polylang 插件自定义翻译

我在这里查看了开发人员文档,认为您需要使用该函数pll_register_string来注册字符串,然后用于pll_e输出该字符串。(更多参数参考:https://polylang.pro/doc/function-reference/#pll_register_string)

pll_e

先前使用 pll_register_string 注册的翻译字符串
用法:

pll_e( $string );

需要的参数

  • ‘$string’ => the string to translate

pll_register_string

允许 Polylang 插件在“翻译”面板中添加自己的字符串。该函数必须在管理端调用(functions.php 文件适用于主题)。可以注册空字符串(例如,当它们来自选项时),但它们不会出现在列表中。
用法:

1pll_register_string( $name, $string, $group, $multiline);
  • ‘$name’ => (required) 为方便排序而提供的名称 (ex: ‘myplugin’)
  • ‘$string’ => (required) 要翻译的字符串
  • ‘$group’ => (optional) 字符串注册的组,默认为 ‘polylang’
  • ‘$multiline’ => (optional) 如果设置为 true,翻译文本字段将为多行,默认为 false

例如,您收到文本“Hello World!”在模板中,然后使用该函数pll_e输出该文本,如下所示:

<span class="h2"> <?php pll_e('Hello world'); ?> </span>

functions.php然后,要在 Polylang 字符串翻译中获取此字符串,您需要使用pll_register_string如下函数将其注册到主题中:

<?php

  add_action('init', function() {pll_register_string('shams-theme', 'Hello World!');
  });

?>
functions.php添加

希望这对您有所帮助,如果您需要更多帮助,请告诉我。

Polylang 切换语言代码如何写?

pll_the_languages

Displays a language switcher.
Usage:

1pll_the_languages( $args);

$args is an optional array parameter. Options are:

  • ‘dropdown’ => displays a list if set to 0, a dropdown list if set to 1 (default: 0)
  • ‘show_names’ => displays language names if set to 1 (default: 1)
  • ‘display_names_as’ => either ‘name’ or ‘slug’ (default: ‘name’)
  • ‘show_flags’ => displays flags if set to 1 (default: 0)
  • ‘hide_if_empty’ => hides languages with no posts (or pages) if set to 1 (default: 1)
  • ‘force_home’ => forces link to homepage if set to 1 (default: 0)
  • ‘echo’ => echoes if set to 1, returns a string if set to 0 (default: 1)
  • ‘hide_if_no_translation’ => hides the language if no translation exists if set to 1 (default: 0)
  • ‘hide_current’=> hides the current language if set to 1 (default: 0)
  • ‘post_id’ => if set, displays links to translations of the post (or page) defined by post_id (default: null)
  • ‘raw’ => use this to create your own custom language switcher (default:0)

Important: You have to output yourself the ul tags if you don’t use the dropdown option.
Examples:

123456789101112<!-- outputs a list of languages names --><ul><?phppll_the_languages(); ?></ul><!-- outputs a flags list (without languages names) --><ul><?phppll_the_languages( array( 'show_flags'=> 1,'show_names'=> 0 ) ); ?></ul><!-- outputs a dropdown list of languages names --><?phppll_the_languages( array( 'dropdown'=> 1 ) ); ?>

If the options are not enough, you can build your own custom language switcher using the ‘raw’ argument:

1$translations= pll_the_languages( array( 'raw'=> 1 ) );

The function will return an array of arrays, one array per language with the following entries:

  • [id] => language id
  • [slug] => language code used in urls
  • [name] => language name
  • [url] => url of the translation
  • [flag] => url of the flag
  • [current_lang] => true if this is the current language, false otherwise
  • [no_translation] => true if there is no available translation, false otherwise

The function is meant to display a language switcher. So it is not availaible on backend. If you need a function to get information about available languages, you should use pll_languages_list() instead.

滚动至顶部
扫描微信二维码联系我们 关闭