E
E
Eldar Amantai2014-07-05 18:09:32
Yii
Eldar Amantai, 2014-07-05 18:09:32

Yii::t how to solve problem with editable texts in project view?

There is a task so that certain texts on the pages of a project (several projects) can be edited.
Example: the title of the widget "They also buy with this product".
Initially, such phrases were wrapped in Yii::t for internationalization, and no one took a steam bath. However, several problems arose:
1. Russian texts (sourceLanguage='ru') of phrases should be changeable
2. Same phrases like "Buy" on different pages should have different entries for different pages. Basically it is solved through different dictionaries.
3. With a large number of translated phrases, do not make 1 request for each word in a non-native language. If you split texts into dictionaries depending on the page's route, it's okay, but how do you force yii::t to prequery the entire dictionary?
The main problem is point 1. The dumbest solution that came to mind is to make sourceLanguage - Mandarin (or any) and prohibit switching the language to it for users, and make Russian the default language. As a result, Russian phrases will be editable... Any ideas?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis, 2014-07-05
@steppefox

The first problem is solved by setting forceTranslation true in the config

'components' => array(
        'messages' => array(
            'forceTranslation' => true,
        ),
...

A
Alexander Zelenin, 2014-07-05
@zelenin

it's up to you, of course, but when making an application with internationalization, I would follow the standard and recommendations of the developers, and would do it based on the English language.
My entire dictionary is obtained in one request and cached into a variable. I mean, this is the default behavior for yii2.

K
kfuntov, 2014-07-05
@kfuntov

Anything can be done by overriding CMessageSource::loadMessages.
For example, categories were not used in one project, but depending on the current parameter in Request, it was necessary to translate the same words differently (within the same language) (it seems like you have a similar problem). So there was some code like this:

<?php
class MessageSource extends CMessageSource
{
    protected function loadMessages($category, $language)
    {
        $config = require(Yii::getPathOfAlias('application.messages') . '/' . $language . '/default.php');

        $childConfig = Yii::getPathOfAlias('application.messages') . '/' . $language . '/children/' . Yii::app()->request->getChildrenTranslateId() . '.php';
        if (file_exists($childConfig)) {
            $vocab = require($childConfig);
            if (is_array($vocab)) {
                $config = array_merge($config, $vocab);
            }
        }

        return $config;
    }
}

Thus, the translation order in the t function was as follows - first we look in the child dictionary, if not there, we look in the default one for the language.
If you need to use categories, you can change the loadMessages method accordingly.
The main idea behind this method would anyway be that array_merge overwrites the values ​​if the keys are the same. Thus, it is possible to make, as it were, inheritance of files with messages.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question