bizley/Yii2-AjaxDropDown

Localization

Closed this issue · 6 comments

Can you write example how to use localization?

Easiest way - prepare translations for the default strings using the default category 'app'.
If you want to use different category set translateCategory parameter.
If you want to add custom messages set local parameter with the array of replaced strings (element key must be the same as in default string and element value is replaced message) i.e.

'local' => ['allRecords' => 'All'] // 'All' instead of default 'All records' for key 'allRecords'

thanks

I made some changes:
in local parameter you just enter true or samething, just to set it.
and in translateCategory you enter translations you prepare in app/messages/local

file: app.php

'All records', 'error' => 'Error', 'minimumCharacters' => 'Type at least {NUM} character(s) to filter the results...', 'next' => 'next', 'noRecords' => 'No matching records found', 'previous' => 'previous', 'recordsContaining' => 'Records containing', ];

and I change something in AjaxDropdown.php

    protected function prepareOptionLocal()
    {
        $local = !empty($this->defaultLocal) ? $this->defaultLocal : [];
        if (!empty($this->local) /*&& is_array($this->local)*/) {
            foreach ($local as $key => $value) {
                if (!empty($value) && is_string($value)) {
                    $local[$key] = Yii::t($this->translateCategory, $key);
                }
            }
        }
...

and then you not need to set array of local for each field

That's one way to do it. With the functionality implemented now you can change texts for multiple widgets on one page at the same time. Also the translation file should be made the classic way - source text => translated text (not text code or something). If you would like to change messages for every widget instance in whole project at once you can always extend this class with other (not default) texts already applied or use Dependency Injection Container.

you make mastake because you forech $this->local insted of $local
and just change array value with translation

Yup, there is the bug. It should be:

$local = $this->defaultLocal;
foreach ($local as $key => $value) {
    if (!empty($this->local[$key]) && is_string($this->local[$key])) {
        $value = $this->local[$key];
    }
    $local[$key] = Yii::t($this->translateCategory, $value);
}

Thanks for pointing that! I'll fix this later today.