You are viewing revision #4 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.
Coming soon.
having problems formatting the code sections
the
languages
array is defined in two places in the above code. This could cause problems as you need to ensure both arrays are the same.If you wish you can create a php file called
languages.php
in theconfig
folder and place the required languages in that. Then call this array from both/config/params.php
and/messages/create_i18n.php
like so:The
/config/languages.php
File:<?php /* List of languages and their codes * * format: * 'Language Code' => 'Language Name', * e.g. * 'fr' => 'Français', * * please use alphabetical order of language code * Use the language name in the "user's" Language * e.g. * 'ja' => '日本の', */ return [ // 'da' => 'Danske', // 'de' => 'Deutsche', 'en-GB' => 'British English', 'en-US' => 'American English', 'es' => 'Español', 'fr' => 'Français', 'it' => 'Italiano', 'ja' => '日本の', // Japanese with the word "Japanese" in Kanji // 'nl' => 'Nederlandse', // 'no' => 'Norsk', // 'pl' => 'Polski', 'pt' => 'Português', // 'ru' => 'Русский', // 'sw' => 'Svensk', // 'zh' => '中国的', ];
The
/config/params.php
File<?php return [ 'adminEmail' => 'admin@example.com', 'senderEmail' => 'noreply@example.com', 'senderName' => 'Example.com mailer', 'languages' => require(__DIR__ . '/languages.php'), ];
The
/messages/create_i18n.php
File<?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' => require(__DIR__ . '/../config/languages.php'), // 'languages' => [ // 'af', 'ar', 'az', 'be', 'bg', 'bs', 'ca', 'cs', 'da', 'de', 'el', 'es', 'et', 'fa', 'fi', 'fr', 'he', 'hi', // 'pt-BR', 'ro', 'hr', 'hu', 'hy', 'id', 'it', 'ja', 'ka', 'kk', 'ko', 'kz', 'lt', 'lv', 'ms', 'nb-NO', 'nl', // 'pl', 'pt', 'ru', 'sk', 'sl', 'sr', 'sr-Latn', 'sv', 'tg', 'th', 'tr', 'uk', 'uz', 'uz-Cy', 'vi', 'zh-CN', // 'zh-TW' // ], // 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' => ['\Yii::t', 'Yii::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 these messages will NOT be removed. 'removeUnused' => false, // boolean, whether to mark messages that no longer appear in the source code. // Defaults to true, which means each of these messages will be enclosed with a pair of '@@' marks. 'markUnused' => true, // array, list of patterns that specify which files (not directories) should be processed. // If empty or not set, all files will be processed. // See helpers/FileHelper::findFiles() for pattern matching rules. // If a file/directory matches both a pattern in "only" and "except", it will NOT be processed. 'only' => ['*.php'], // array, list of patterns that specify which files/directories should NOT be processed. // If empty or not set, all files/directories will be processed. // See helpers/FileHelper::findFiles() for pattern matching rules. // If a file/directory matches both a pattern in "only" and "except", it will NOT be processed. 'except' => [ '.*', '/.*', '/messages', '/tests', '/runtime', '/vendor', '/BaseYii.php', ], // '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, /* // File header used in generated messages files 'phpFileHeader' => '', // PHPDoc used for array of messages with generated messages files 'phpDocBlock' => null, */ /* // Message categories to ignore 'ignoreCategories' => [ 'yii', ], */ /* // 'db' output format is for saving messages to database. 'format' => 'db', // Connection component to use. Optional. 'db' => 'db', // Custom source message table. Optional. // 'sourceMessageTable' => '{{%source_message}}', // Custom name for translation message table. Optional. // 'messageTable' => '{{%message}}', */ /* // 'po' output format is for saving messages to gettext po files. 'format' => 'po', // Root directory containing message translations. 'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . 'messages', // Name of the file that will be used for translations. 'catalog' => 'messages', // boolean, whether the message file should be overwritten with the merged messages 'overwrite' => true, */ ];
There is a bug in the create_i18n.php file in the above comment. It returns both the array key and the value. We only need the array key.
Also, after running
./yii message ./messages/create_i18n.php
you will need to delete the Default Language folder (usuallyen
oren-GB
) from the/messages/
directory. It is created by this command but is only required for the languages dropdown menu but not the translations,<?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']. // The languages array is in `/config/languages.php` // We need just the Array Keys for this file but, after running `./yii message ./messages/create_i18n.php` // Remove the Default Language folder (usually `en` or `en-GB`) from the `/messages/` directory 'languages' => array_keys(require(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'languages.php')), // Original list of languages. These are the languages Yii "knows about" // 'languages' => [ // 'af', 'ar', 'az', 'be', 'bg', 'bs', 'ca', 'cs', 'da', 'de', 'el', 'es', 'et', 'fa', 'fi', 'fr', 'he', 'hi', // 'pt-BR', 'ro', 'hr', 'hu', 'hy', 'id', 'it', 'ja', 'ka', 'kk', 'ko', 'kz', 'lt', 'lv', 'ms', 'nb-NO', 'nl', // 'pl', 'pt', 'ru', 'sk', 'sl', 'sr', 'sr-Latn', 'sv', 'tg', 'th', 'tr', 'uk', 'uz', 'uz-Cy', 'vi', 'zh-CN', // 'zh-TW' // ], // 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' => ['\Yii::t', 'Yii::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 these messages will NOT be removed. 'removeUnused' => false, // boolean, whether to mark messages that no longer appear in the source code. // Defaults to true, which means each of these messages will be enclosed with a pair of '@@' marks. 'markUnused' => true, // array, list of patterns that specify which files (not directories) should be processed. // If empty or not set, all files will be processed. // See helpers/FileHelper::findFiles() for pattern matching rules. // If a file/directory matches both a pattern in "only" and "except", it will NOT be processed. 'only' => ['*.php'], // array, list of patterns that specify which files/directories should NOT be processed. // If empty or not set, all files/directories will be processed. // See helpers/FileHelper::findFiles() for pattern matching rules. // If a file/directory matches both a pattern in "only" and "except", it will NOT be processed. 'except' => [ '.*', '/.*', '/messages', '/tests', '/runtime', '/vendor', '/BaseYii.php', ], // '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, /* // File header used in generated messages files 'phpFileHeader' => '', // PHPDoc used for array of messages with generated messages files 'phpDocBlock' => null, */ /* // Message categories to ignore 'ignoreCategories' => [ 'yii', ], */ /* // 'db' output format is for saving messages to database. 'format' => 'db', // Connection component to use. Optional. 'db' => 'db', // Custom source message table. Optional. // 'sourceMessageTable' => '{{%source_message}}', // Custom name for translation message table. Optional. // 'messageTable' => '{{%message}}', */ /* // 'po' output format is for saving messages to gettext po files. 'format' => 'po', // Root directory containing message translations. 'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . 'messages', // Name of the file that will be used for translations. 'catalog' => 'messages', // boolean, whether the message file should be overwritten with the merged messages 'overwrite' => true, */ ];
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.