#php #gettext #code-snippets
#php #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 amp;amp;($min_tickets > 0) ) : ?>
<p class="min-pariticipants"><?php printf( __( "This lottery has a minimum of %d tickets", 'wc_lottery'), $min_tickets ); ?></p>
<?php endif; ?>
<?php if( $max_tickets amp;amp;( $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 , 'wc_lottery' ) ,$lottery_num_winners ) ; ?></p>
<?php endif; ?>
Ответ №1:
Вы могли бы (но не должны!) Просто сделать слово «лотерея» или «конкурс» заполнителем:
<?php printf(__("This %s is limited to %s tickets.", $type, $max_tickets))?>
Но не делайте этого!
В общем, плохая идея собирать переводимые строки в коде, потому что это не будет работать для других языков. Например, два предложения будут переведены на немецкий язык как:
- Diese Lotterie ist auf %s Tickets begrenzt.
- Dieser Wettbewerb ist auf %s Tickets begrenzt.
Слово перед лотереей / соревнованием также меняется в немецком языке, потому что существительные имеют разные роды.
Вместо этого будьте более подробными, как это:
<?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
файл будет содержать две разные строки, и их можно перевести на любой язык.