From 806dbf9b457dd492b43ecbe9bc53fd03ec28d86a Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 26 Jan 2014 16:40:53 +0400 Subject: [PATCH 1/2] i18n docs added --- docs/guide/i18n.md | 98 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/docs/guide/i18n.md b/docs/guide/i18n.md index efc76933fa..f2609b5047 100644 --- a/docs/guide/i18n.md +++ b/docs/guide/i18n.md @@ -284,4 +284,102 @@ Formatters In order to use formatters you need to install and enable [intl](http://www.php.net/manual/en/intro.intl.php) PHP extension. +Examples +-------- + +###Translating module messages + +If you want to translate messages for module and you dont use one translation file for all messages, you can make it like following: + +```php +registerTranslations(); + } + + public function registerTranslations() + { + $i18n = Yii::$app->i18n; + $i18n->translations['modules/users/*'] = [ + 'class' => 'yii\i18n\PhpMessageSource', + 'sourceLanguage' => 'en', + 'basePath' => '@app/modules/users/messages', + 'fileMap' => [ + 'modules/users/validation' => 'validation.php', + 'modules/users/form' => 'form.php', + ... + ], + ]; + } + + public static function t($category, $message, $params = [], $language = null) + { + return Yii::t('modules/users/' . $category, $message, $params, $language); + } + +} +``` + +In the example above we are using wildcard for matching and then filtering each category per needed file. + +###Translating widgets messages + +Same rules can be applied for widgets too, for example: + +```php +registerTranslations(); + } + + public function registerTranslations() + { + $i18n = Yii::$app->i18n; + $i18n->translations['widgets/menu/*'] = [ + 'class' => 'yii\i18n\PhpMessageSource', + 'sourceLanguage' => 'en', + 'basePath' => '@app/widgets/menu/messages', + 'fileMap' => [ + 'widgets/menu/messages' => 'messages.php', + ], + ]; + } + + public function run() + { + echo $this->render('index'); + } + + public static function t($category, $message, $params = [], $language = null) + { + return Yii::t('widgets/menu/' . $category, $message, $params, $language); + } + +} +``` + +> **Note**: For widgets you also can use i18n views, same rules as for controllers are applied to them too. + TBD: provided classes overview. From b499e630c349372ae71c41334e3562ade6dd3e53 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 26 Jan 2014 17:00:09 +0400 Subject: [PATCH 2/2] docs fixed --- docs/guide/i18n.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/guide/i18n.md b/docs/guide/i18n.md index f2609b5047..d772702419 100644 --- a/docs/guide/i18n.md +++ b/docs/guide/i18n.md @@ -289,7 +289,7 @@ Examples ###Translating module messages -If you want to translate messages for module and you dont use one translation file for all messages, you can make it like following: +If you want to translate messages for a module and avoid using a single translation file for all messages, you can make it like the following: ```php i18n; - $i18n->translations['modules/users/*'] = [ + Yii::$app->i18n->translations['modules/users/*'] = [ 'class' => 'yii\i18n\PhpMessageSource', 'sourceLanguage' => 'en', 'basePath' => '@app/modules/users/messages',