Internaționalizare

Mesaje

Pentru internaționalizare se folosesc facilitățile oferite de Frameworkul Yii2, vezi tutorialul.

Widgetul de comutare limbă utilizat – LanguagePicker – setează limba curentă, din \Yii::$app->language;

Un mesaj ce trebuie tradus se scrie:

Yii::t('app', 'This is the original text');

așa încât să poată fi găsit la parsarea sursei de către PoEdit (GetText utility) și considerat pentru traducere. Frameworkul folosește fișierele cu traducerile (.po și .mo – sursă, compilat) din folderul site/common/messages/ro_RO/ app.po respectiv app.mo.

 Formate

În privința internaționalizării celorlalte aspecte legate de simbol valutar, format număr, format dată – ea e bazată pe biblioteca PHP_intl care la rândul ei respectă standardele internaționale ICU – vezi manualul intl.

Formatare număr

... simplu: Yii::$app->formatter->asDecimal($value,2); // cu două zecimale
... într-un GridView cu 'model' => $model coloana...

'total' => [
  'label' => Yii::t('app', 'Total'),
  'atribute' => 'total',  
  'value' => function ($model) {
    return Yii::$app->formatter->asDecimal($model->total,2);
  },  
],
... câmp cu formatare valutară

$form->field($model, 'total')->widget(MaskMoney::classname(), [
 'disabled' => 'true', //sau false
 'options' => [
   'class' => 'text-right'
 ],
]);

 

Formatare dată

... simplu: Yii::$app->formatter->asDate(substr($model->date, 0,10)); // unde $model->date e sir in formatul intern Y-m-d H:m:s a

... câmp cu selector de dată

$form->field($model, 'issueDate')->widget(DateControl::classname(), ['type' => DateControl::FORMAT_DATE]);
... într-un GridView cu 'model' => $model coloana...

'date' => [
   'label' => Yii::t('app', 'Date'),
   'attribute' => 'date',
   'format' => ['date'],
...

 

 

Câteva exemple utile:

NumberFormatter

http://ro1.php.net/manual/en/class.numberformatter.php

if you want to localize a number use: 

<?php 
$a = new \NumberFormatter("it-IT", \NumberFormatter::DECIMAL); 
echo $a->format(12345.12345) . "<br>"; // outputs 12.345,12 
$a->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0); 
$a->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 100); // by default some locales got max 2 fraction digits, that is probably not what you want 
echo $a->format(12345.12345) . "<br>"; // outputs 12.345,12345 
?> 

if you want to print money use: 

<?php 
$a = new \NumberFormatter("it-IT", \NumberFormatter::CURRENCY); 
echo $a->format(12345.12345) . "<br>"; // outputs €12.345,12 
?> 

if you have money data stored as (for example) US dollars and you want to print them using the it-IT notation, you need to use 

<?php 
$a = new \NumberFormatter("it-IT", \NumberFormatter::CURRENCY); 
echo $a->formatCurrency(12345, "USD") . "<br>"; // outputs $ 12.345,00 and it is formatted using the italian notation (comma as decimal separator) 
?> 

another useful example about currency (how to obtain the currency name by a locale string): 

<?php 
$frontEndFormatter = new \NumberFormatter("it-IT", \NumberFormatter::CURRENCY); 
$adminFormatter = new \NumberFormatter("en-US", \NumberFormatter::CURRENCY); 
$symbol = $adminFormatter->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL); // got USD 
echo $frontEndFormatter->formatCurrency(12345.12345,  $symbol) . "<br>"; 
?>

<?php
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format(123456); // output: one hundred twenty-three thousand four hundred fifty-six

?> 

IntlDateFormatter

http://ro1.php.net/manual/en/class.intldateformatter.php

<?php
/* default timezone is irrelevant; timezone taken from the object */
ini_set('date.timezone', 'UTC');
/* default locale is taken from this ini setting */
ini_set('intl.default_locale', 'fr_FR');

$cal = IntlCalendar::fromDateTime("2013-06-06 17:05:06 Europe/Dublin");
echo "default:\n\t",
        IntlDateFormatter::formatObject($cal),
        "\n";

echo "long \$format (full):\n\t",
        IntlDateFormatter::formatObject($cal, IntlDateFormatter::FULL),
        "\n";

echo "array \$format (none, full):\n\t",
        IntlDateFormatter::formatObject($cal, array(
                IntlDateFormatter::NONE,
                IntlDateFormatter::FULL)),
        "\n";

echo "string \$format (d 'of' MMMM y):\n\t",
        IntlDateFormatter::formatObject($cal, "d 'of' MMMM y", 'en_US'),
        "\n";

echo "with DateTime:\n\t",
        IntlDateFormatter::formatObject(
                new DateTime("2013-09-09 09:09:09 Europe/Madrid"),
                IntlDateFormatter::FULL,
                'es_ES'),
        "\n";
/* output:
default:
    6 juin 2013 17:05:06
long $format (full):
    jeudi 6 juin 2013 17:05:06 heure d’été irlandaise
array $format (none, full):
    17:05:06 heure d’été irlandaise
string $format (d 'of' MMMM y):
    6 of June 2013
with DateTime:
    lunes, 9 de septiembre de 2013 09:09:09 Hora de verano de Europa central
*/

 

About

Marele Po, UX Expert und Business Specialist

Categories: i18n, UI/UX |

Leave a Reply

Your email address will not be published. Required fields are marked *

[TOP]