我想翻译我插件中设置页面上按钮样式下拉列表中的所有项目。
通常,当我想翻译一些东西时,我会使用以下模式:
__( \'Theme style\', \'qty-increment-buttons-for-woocommerce\' )
但是,这不是纯文本,而是我在默认数组中使用的设置:
$defaults = array ( \'qib_auto_table\' => false, \'qib_merge_buttons\' => true, \'qib_button_height\' => 35,
\'qib_button_width\' => 35, \'qib_button_style\' => \'Silver\', \'qib_quantity_field_width\' => 52 );
然后在我的插件中进行如下检查:
switch ( $args[\'button_style\'] ) {
case \'Theme style\':
我可以翻译这些选项并使用我的正常翻译模式(也在$items数组中)引用它们吗?或者,如果有人用一种语言保存设置,然后更改网站的语言,它可能会中断?
public function qib_button_style_callback() {
$items = array (\'Theme style\', \'Silver\', \'Black\', \'Orange\');
echo \'<select id="qib_button_style" name="qib_settingz[qib_button_style]">\';
foreach($items as &$item) {
$selected = (isset( $this->options[\'qib_button_style\'])) && ( $item == $this->options[\'qib_button_style\']) ? \'selected="selected"\' : \'\';
$item = __( $item, \'qty-increment-buttons-for-woocommerce\' );
printf(
\'<option value="%1$s" %2$s>%1$s</option>\',
esc_attr($item),
$selected
);
}
printf(
\'</select>
<p class="description">%1$s</p>\',
__( \'To use button colors from your theme, choose "Theme style". Otherwise plugin will use its own style to color increment buttons. It includes background, font, hover background and focus outline. Also quanity input field\\\'s border is colored. If for example custom background color is needed, you can override rule with child theme CSS.\', \'qty-increment-buttons-for-woocommerce\' )
);
}
最合适的回答,由SO网友:Jacob Peattie 整理而成
我的建议是不要使用标签作为下拉列表中的值。该值存储在数据库中,并在模板中以编程方式使用,因此无需本地化。然后你可以翻译标签,没有任何副作用。
public function qib_button_style_callback() {
$items = array(
\'theme-style\' => __( \'Theme style\', \'qty-increment-buttons-for-woocommerce\' ),
\'silver\' => __( \'Silver\', \'qty-increment-buttons-for-woocommerce\' ),
\'black\' => __( \'Black\' \'qty-increment-buttons-for-woocommerce\' ),
\'orange\' => __( \'Orange\' \'qty-increment-buttons-for-woocommerce\' ),
);
echo \'<select id="qib_button_style" name="qib_settingz[qib_button_style]">\';
foreach( $items as $value => $option ) {
printf(
\'<option value="%1$s" %2$s>%3$s</option>\',
esc_attr( $value ),
selected( $value, $this->options[\'qib_button_style\'], false ),
esc_html( $option )
);
}
printf(
\'</select>
<p class="description">%1$s</p>\',
__( \'To use button colors from your theme, choose "Theme style". Otherwise plugin will use its own style to color increment buttons. It includes background, font, hover background and focus outline. Also quanity input field\\\'s border is colored. If for example custom background color is needed, you can override rule with child theme CSS.\', \'qty-increment-buttons-for-woocommerce\' )
);
}
然后,您可以在模板中检查值,而无需担心翻译:
switch ( $args[\'button_style\'] ) {
case \'theme-style\':
并设置相应的默认值:
\'qib_button_style\' => \'silver\'
还要注意,我使用了一个WordPress函数,
selected()
, 这样可以更轻松地设置下拉列表的当前值。
另一件需要记住的事情是,不能将变量传递给转换函数,如__()
. 您只能传递字符串。所以这是不正确的:
__( $item, \'qty-increment-buttons-for-woocommerce\' );
马克·贾奎斯在他的帖子中描述了原因
Translating WordPress Plugins and Themes: Don’t Get Clever:
看,PHP不是唯一需要解析出可翻译字符串的东西。GNU gettext还需要解析字符串,以便为翻译人员提供空白的翻译文件。GNU gettext is not a PHP parser. 它无法读取变量或常量。它只读取字符串。因此,文本域字符串需要保持硬编码为实际引用字符串。\'my-plugin-name\'
.