Localization - Any tipps or best practices?

[font=“Lucida Sans Unicode”]I am wondering how you work with different INPUT FORMATS per user’s locale in your apps.

E.g. input type=date.

i want to display/enter dates in the user’s current setting (dd.mm.yyyy).

E.g. input type=decimal.

here i face different decimal points, thousandsseparator, precision.

what i usually do is:

  1. write app in english with translation support

  2. store suspect data always in ISO format

  3. use a language route extension to switch languages

  4. set formats:

4a. default (yyyy-mm-dd)

4b. detected language default (dd.mm.yyyy)

4c. user setting (dd.mm.yy)

  1. generate a javascript configuration for my presentational input boxes

  2. use knockoutjs for bidirectional transformation from ISO to custom format with a hidden input

it seems a little bit too much, because what i originally intended to do was something like:


$form->field($model, 'mydecimal')->decimal()

or


$form->field($model, 'mydate')->datetime('short')

… and leave it all to (detected) defaults

Ideas welcome!!!

[/font]

Hi!

Read "Internatonalization":

http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html

To understand all the basics regarding translations.

And now to your actual problem.

After that read "Data Formatter":

http://www.yiiframework.com/doc-2.0/guide-output-formatter.html

That is the component which makes it possible to have different time/date/decimal & thouthand formats for different languages.

Regards

[font="Verdana"]

Thank’s for your quick response!

I am aware of these guides, but i don’t see how this can help me getting formatted inputs from forms.

ActiveForm -> field -> input -> display and enter 1.234,56

now get the value in ISO, 1234.56 and do some calculations, store to DB.

Missing something else?[/font]

Hello again,

Ah I think now I understood your problem correctly!

Not sure if this is the best way but lets say you dynamically receive decimal input in different languages.

Like for example this are the different inputs you receive from a field:




US: 12,345,678,900.99

DE: 12.345.678.900,99

RU: 12 345 678 900,99



You always want it in this format before saving.




12345678900.99



Now you could do, for example:




// current app-language 

$lang = Yii::$app->language;


// receive current langs grouping / decimal seperator from INTL

$intl = new NumberFormatter($lang, NumberFormatter::DECIMAL);

$group_seperator    = $intl->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL);

$decimal_seperator  = $intl->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);


// replace the grouping and decimal seperators 

$input = $myFormInput; // receive your form-input here !

$input = str_replace($group_seperator, '', $input);

$input = str_replace($decimal_seperator, '.', $input);


// now $input should always be 12345678900.99

echo $input; 



Hope this helps.

Regards

[font="Verdana"]

@MetaCrawler

here is my solution for raw-data/locale display:

can you try this?

feedback welcome!!!

[/font]