使用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文件将包含两个不同的字符串,并且可以将它们翻译成任何语言。