Skip to content

Commit

Permalink
i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
v0lume committed Mar 13, 2015
1 parent 47a0c27 commit b343de9
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 4 deletions.
25 changes: 24 additions & 1 deletion MetaTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,29 @@ class MetaTags extends Widget
public function init()
{
parent::init();
self::registerTranslations();

if (!$this->model->getBehavior($this->behaviorName))
{
throw new Exception("Модель должна иметь поведение {$this->behaviorName}", 500);
throw new Exception(self::t('messages', 'widget_behavior_exception {behaviorName}', ['behaviorName' => $this->behaviorName]), 500);
}
}


public static function registerTranslations()
{
$i18n = Yii::$app->i18n;
$i18n->translations['metaTags/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'sys',
'basePath' => '@vendor/v0lume/yii2-meta-tags/messages',
'fileMap' => [
'metaTags/messages' => 'messages.php',
],
];
}


public function run()
{
$model = new MetaTag;
Expand All @@ -54,4 +69,12 @@ public function run()
'form' => $this->form,
]);
}


public static function t($category, $message, $params = [], $language = null)
{
self::registerTranslations();

return Yii::t('metaTags/' . $category, $message, $params, $language);
}
}
50 changes: 50 additions & 0 deletions messages/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
return [
// string, required, root directory of all source files
'sourcePath' => __DIR__ . DIRECTORY_SEPARATOR . '..',
// array, required, list of language codes that the extracted messages
// should be translated to. For example, ['zh-CN', 'de'].
'languages' => ['en', 'ru', 'de', 'zh', 'fr', 'it', 'hr', 'pt'],
// string, the name of the function for translating messages.
// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
// translated. You may use a string for single function name or an array for
// multiple function names.
'translator' => 'MetaTags::t',
// boolean, whether to sort messages by keys when merging new messages
// with the existing ones. Defaults to false, which means the new (untranslated)
// messages will be separated from the old (translated) ones.
'sort' => false,
// boolean, whether to remove messages that no longer appear in the source code.
// Defaults to false, which means each of these messages will be enclosed with a pair of '@@' marks.
'removeUnused' => false,
// array, list of patterns that specify which files/directories should NOT be processed.
// If empty or not set, all files/directories will be processed.
// A path matches a pattern if it contains the pattern string at its end. For example,
// '/a/b' will match all files and directories ending with '/a/b';
// the '*.svn' will match all files and directories whose name ends with '.svn'.
// and the '.svn' will match all files and directories named exactly '.svn'.
// Note, the '/' characters in a pattern matches both '/' and '\'.
// See helpers/FileHelper::findFiles() description for more details on pattern matching rules.
'only' => ['*.php'],
// array, list of patterns that specify which files (not directories) should be processed.
// If empty or not set, all files will be processed.
// Please refer to "except" for details about the patterns.
// If a file/directory matches both a pattern in "only" and "except", it will NOT be processed.
'except' => [
'.DS_Store',
'.svn',
'.git',
'.gitignore',
'.gitkeep',
'.hgignore',
'.hgkeep',
'/messages',
'/migrations',
],
// 'php' output format is for saving messages to php files.
'format' => 'php',
// Root directory containing message translations.
'messagePath' => __DIR__,
// boolean, whether the message file should be overwritten with the merged messages
'overwrite' => true,
];
24 changes: 24 additions & 0 deletions messages/de/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'model_description' => 'Beschreibung',
'model_keywords' => 'Schlüsselwörter',
'model_title' => 'Titel',
'widget_behavior_exception {behaviorName}' => 'Modell muss verhalten {behaviorName}',
];
24 changes: 24 additions & 0 deletions messages/en/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'model_description' => 'Description',
'model_keywords' => 'Keywords',
'model_title' => 'Title',
'widget_behavior_exception {behaviorName}' => 'Model must have behavior {behaviorName}',
];
24 changes: 24 additions & 0 deletions messages/fr/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'model_description' => 'Description',
'model_keywords' => 'Mots-clés',
'model_title' => 'Titre',
'widget_behavior_exception {behaviorName}' => 'Modèle doit avoir un comportement {behaviorName}',
];
24 changes: 24 additions & 0 deletions messages/hr/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'model_description' => 'Opis',
'model_keywords' => 'Ključne riječi',
'model_title' => 'Naslov',
'widget_behavior_exception {behaviorName}' => 'Model mora imati ponašanje {behaviorName}',
];
24 changes: 24 additions & 0 deletions messages/it/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'model_description' => 'Descrizione',
'model_keywords' => 'Parole chiave',
'model_title' => 'Titolo',
'widget_behavior_exception {behaviorName}' => 'Modello deve avere un comportamento {behaviorName}',
];
24 changes: 24 additions & 0 deletions messages/pt/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'model_description' => 'Descrição',
'model_keywords' => 'Palavras-chave',
'model_title' => 'Título',
'widget_behavior_exception {behaviorName}' => 'Modelo deve ter comportamento {behaviorName}',
];
24 changes: 24 additions & 0 deletions messages/ru/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'model_description' => 'Описание',
'model_keywords' => 'Ключевые слова',
'widget_behavior_exception {behaviorName}' => 'Модель должна иметь поведение {behaviorName}',
'model_title' => 'Заголовок',
];
24 changes: 24 additions & 0 deletions messages/zh/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'model_description' => '描述',
'model_keywords' => '關鍵詞',
'model_title' => '稱號',
'widget_behavior_exception {behaviorName}' => '模型必須有行為 {behaviorName}',
];
7 changes: 4 additions & 3 deletions models/MetaTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Yii;
use yii\behaviors\TimestampBehavior;
use v0lume\yii2\metaTags\MetaTags;

class MetaTag extends \yii\db\ActiveRecord
{
Expand Down Expand Up @@ -41,9 +42,9 @@ public function rules()
public function attributeLabels()
{
return [
'title' => 'Заголовок',
'keywords' => 'Ключевые слова',
'description' => 'Описание',
'title' => MetaTags::t('messages', 'model_title'),
'keywords' => MetaTags::t('messages', 'model_keywords'),
'description' => MetaTags::t('messages', 'model_description'),
];
}

Expand Down

0 comments on commit b343de9

Please sign in to comment.