如何解决使用gettext过滤器更改单词
我对PHP真的很陌生,所以请保持柔和:-) 我想使用代码片段并将“彩票”一词更改为“竞争”。
我会使用gettext
过滤器吗?
谢谢
<?php
/**
* Lottery info template
*
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product,$post;
$min_tickets = $product->get_min_tickets();
$max_tickets = $product->get_max_tickets();
$lottery_participants_count = !empty($product->get_lottery_participants_count()) ? $product->get_lottery_participants_count() : '0';
$lottery_dates_to = $product->get_lottery_dates_to();
$lottery_dates_from = $product->get_lottery_dates_from();
$lottery_num_winners = $product->get_lottery_num_winners();
?>
<p class="lottery-end"><?php echo __( 'Lottery ends:','wc_lottery' ); ?> <?php echo date_i18n( get_option( 'date_format' ),strtotime( $lottery_dates_to )); ?> <?php echo date_i18n( get_option( 'time_format' ),strtotime( $lottery_dates_to )); ?> <br />
<?php printf(__('Timezone: %s','wc_lottery'),get_option('timezone_string') ? get_option('timezone_string') : __('UTC+','wc_lottery').get_option('gmt_offset')) ?>
</p>
<?php if($min_tickets &&($min_tickets > 0) ) : ?>
<p class="min-pariticipants"><?php printf( __( "This lottery has a minimum of %d tickets",$min_tickets ); ?></p>
<?php endif; ?>
<?php if( $max_tickets &&( $max_tickets > 0 ) ) : ?>
<p class="max-pariticipants"><?php printf( __( "This lottery is limited to %s tickets",'wc_lottery' ),$max_tickets ) ; ?></p>
<?php endif; ?>
<p class="cureent-participating"> <?php _e( 'Tickets sold:','wc_lottery' )?> <?php echo $lottery_participants_count ;?></p>
<?php if( $lottery_num_winners > 0 ) : ?>
<p class="max-pariticipants"><?php printf( _n( "This lottery will have %d winner","This lottery will have %d winners",$lottery_num_winners,$lottery_num_winners ) ; ?></p>
<?php endif; ?>
解决方法
您可以(但不应!)只需将“彩票”或“竞争”一词用作占位符:
<?php printf(__("This %s is limited to %s tickets.",$type,$max_tickets))?>
但是不要这样做!
通常,在代码中组装可翻译字符串是一个坏主意,因为这不适用于其他语言。例如,这两个句子将被翻译成德语:
- Diese彩票 ist auf%s门票开始。
- Dieser Wettbewerb ist auf%s门票开始。
彩票中的之前一词在德语中也会发生变化,因为名词的性别不同。
反而更像这样:
<?php
if ($type == 'lottery') {
printf(__("This lottery is limited to %s tickets.",$max_tickets));
} else {
printf(__("This competition is limited to %s tickets.",$max_tickets));
}
?>
现在,您的po
文件将包含两个不同的字符串,并且可以将它们翻译成任何语言。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。