do_action( 'wpml_register_single_string', string $context, string $name, string $value )
- 类型
- action
- 类别
- 插入内容
- WPML 版本
- 3.2
说明
注册单个文本字符串以进行翻译(区别于作为包的一部分的字符串)。此操作通常用于用户输入文本,也称为“动态文本字符串”。
要获取字符串翻译,请参阅:wpml_translate_single_string
要注册作为包的一部分的文本字符串,请参阅:wpml_register_string
注意:此钩子需要 WPML 字符串翻译模块
参数
- $context
- (字符串)(必填)此值为您要注册的字符串提供上下文。这通常是插件或主题的名称,采用人类可读的格式
- $name
- (字符串)(必填)字符串的名称,有助于译员了解正在翻译的内容
- $value
- (字符串)(必填)需要翻译的字符串
- $allow_empty_value
- (布尔值:默认为 false)(可选)是否允许空值
- $source_lang_code
- (字符串)(可选)已注册字符串的语言代码
使用示例
假设您创建了一个带有标题输入字段和其他一些选项的自定义小工具。您可以使用 apply_filters 注册小工具标题以供翻译,但您也希望注册可保存在小工具其他选项字段中的输入文本。
最好在保存或更新小工具选项时(即在小工具的 ‘update’ 方法中)注册输入文本以供翻译。
下面是代码示例。我们的示例自定义小工具具有一个标题、一个单行输入字段和一个文本区域。
示例
function update($new_instance, $old_instance){
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['custom_input'] = strip_tags($new_instance['custom_input']);
$instance['custom_textarea'] = $new_instance['custom_textarea'];
//WPML
/**
* register strings for translation
*/
do_action( 'wpml_register_single_string', 'Widgets', 'Custom Widget - input field', $instance['custom_input'] );
do_action( 'wpml_register_single_string', 'Widgets', 'Custom Widget - textarea field', $instance['custom_textarea'] );
//WPML
return $instance;
}
上面的代码告诉 WPML,输入字段和文本区域的用户输入文本需要翻译。这些文本将分别在 Widgets 字符串上下文中注册,名称分别为 Custom Widget – input field 和 Custom Widget – textarea field。
相关钩子
wpml_set_element_language_details, wpml_make_post_duplicates, wpml_admin_make_post_duplicates, wpml_copy_post_to_language, wpml_delete_package, wpml_import_get_trid_from_imported_translations, wpml_register_string, wpml_show_package_language_ui, wpml_delete_package_action