📜 ⬆️ ⬇️

PHP code variant to output a string in the plural

As usual for an international project, a function was needed to display a word in various variants of the plural form.
The project already has its own language loading option, so using the standard gettext would change much of the code.

So a function was written that, for a supported set of 27 languages, implemented the necessary functionality.

Its code is presented below.
')


1. It needs to define in the global array $ lang 6 pairs of key-values ​​of the type:
<?php
$lang['comment_form_0'] = '';
$lang['comment_form_1'] = '';
$lang['comment_form_2'] = '';
$lang['comment_form_3'] = '';
$lang['comment_form_4'] = '';
$lang['comment_form_5'] = '';
?>


The number 6 is the maximum number of forms among all supported languages.
The language with these 6 forms is Arabic.

2. The language is encoded by the first-level domain for the relevant country (yes, I know that it’s correct to use the ISO language identifier).

One of the obvious disadvantages is that there is no explicit binding of the form number to the number of elements,
which requires an understanding of the algorithm from the translator.

Well, the code itself:

Copy Source | Copy HTML<br/> <?php <br/> /** <br/> * get localized text by key in multi variant cases. <br/> * Attention: Lang ID is first-level domain for country but not ISo lang identificator! <br/> * <br/> * @param string $key - base part of key <br/> * @param int $n - number of items <br/> ( special case == 0 -- it means that text will be fetched simply by $key) <br/> * @param string|null - lang ID which will be used for plural form check <br/> * <br/> * @return string - localized text <br/> */ <br/> function get_lang( $key , $n = null, $lang_id = null ) {<br/> <br/> global $lang ;<br/> if ( null === $n ) {<br/> return $lang [ $key ];<br/> }<br/> <br/> $key_postfix = '_form_' ;<br/> <br/> switch ( $lang_id ) {<br/> <br/> case 'ar' : // arabic, nplurals=6 <br/> $s = $key_postfix .( ( $n == 0 ) ? '0' : ( ( $n == 1 ) ? 1 : ( ( $n == 2 ) ? 2 : ( ( ( $n % 100 >= 3 ) && ( $n % 100 <= 10 ) ) ? '3' : ( ( ( $n % 100 >= 11 ) && ( $n % 100 <= 99 ) ) ? '4' : '5' ) ) ) ) );<br/> $localized = $lang [ $key . $s ];<br/> break ;<br/> <br/> case 'cz' : // czech, nplurals=3 <br/> $s = $key_postfix .( ( $n == 1 ) ? '0' : ( $n >= 2 && $n <= 4 ) ? '1' : '2' );<br/> $localized = $lang [ $key . $s ];<br/> break ;<br/> <br/> case 'de' : // german <br/> case 'bg' : // bulgarian <br/> case 'gr' : // greek <br/> case 'en' : // english <br/> case 'es' : // espanol <br/> case 'ee' : // estonian <br/> case 'il' : // hebrew <br/> case 'it' : // italian <br/> case 'mn' : // mongolian <br/> case 'nl' : // dutch <br/> case 'sq' : // albainian <br/> case 'my' : // malay <br/> // nplurals=2; <br/> $s = $key_postfix .( ( $n != 1 ) ? '0' : '1' );<br/> $localized = $lang [ $key . $s ];<br/> break ;<br/> <br/> case 'pl' : // polskiy, nplurals=3 <br/> $s = $key_postfix .( ( $n == 1 ) ? '0' : ( ( $n % 10 >= 2 ) && ( $n % 10 <= 4 ) && ( $n % 100 < 10 || $n % 100 >= 20 ) ) ? '1' : '2' );<br/> $localized = $lang [ $key . $s ];<br/> break ;<br/> <br/> case 'ru' : // russian, nplurals=3 <br/> $s = $key_postfix .( (( $n % 10 == 1 ) && ( $n % 100 != 11 )) ? '0' : (( ( $n % 10 >= 2 ) && ( $n % 10 <= 4 ) && ( $n % 100 < 10 || $n % 100 >= 20 )) ? '1' : '2' ) );<br/> $localized = $lang [ $key . $s ];<br/> break ;<br/> <br/> case 'sk' : // Slovak, nplurals=3 <br/> $s = $key_postfix .( ( $n == 1 ) ? '1' : ( ( $n >= 2 && $n <= 4 ) ? '2' : '0' ) );<br/> $localized = $lang [ $key . $s ];<br/> break ;<br/> <br/> case 'fa' : // farsi <br/> case 'ja' : // japan <br/> case 'tr' : // turkish <br/> case 'vn' : // vietnamese <br/> case 'cn' : // chinese + <br/> case 'tw' : // tradional Chinese (?) <br/> case 'kz' : // Kazakh <br/> // nplurals=1 <br/> $s = $key_postfix . '0' ;<br/> $localized = $lang [ $key . $s ];<br/> break ;<br/> <br/> case 'ua' : // ukrainian, nplurals=3 <br/> $s = $key_postfix .( ( $n % 10 == 1 && $n % 100 != 11 ) ? '0' : ( $n % 10 >= 2 && $n % 10 <= 4 && ( $n % 100 < 10 || $n % 100 >= 20 ) ) ? '1' : '2' );<br/> $localized = $lang [ $key . $s ];<br/> break ;<br/> <br/> case 'lt' : // Lithuanian, nplurals=3 <br/> $s = $key_postfix .( ( $n % 10 == 1 && $n % 100 != 11 ) ? '0' : ( $n % 10 >= 2 && ( $n % 100 < 10 || $n % 100 >= 20 ) ) ? '1' : '2' );<br/> $localized = $lang [ $key . $s ];<br/> break ;<br/> <br/> case 'fr' : // french, nplurals=2 <br/> $s = $key_postfix .( $n > 1 ? '0' : '1' );<br/> $localized = $lang [ $key . $s ];<br/> break ;<br/> }<br/> <br/> return $localized ;<br/>}<br/> ?> <br/>


Porting to another language, I think, will be easy.

But if the project allows, then I recommend, of course, using standard solutions - in PHP it is gettext.

Source: https://habr.com/ru/post/56864/