Answer the question
In order to leave comments, you need to log in
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
The first problem is solved by setting forceTranslation true in the config
'components' => array(
'messages' => array(
'forceTranslation' => true,
),
...
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.
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;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question