{"id":14966548,"url":"https://github.com/jql/i18ntutorial","last_synced_at":"2026-01-24T15:07:10.275Z","repository":{"id":209506529,"uuid":"724222322","full_name":"JQL/i18ntutorial","owner":"JQL","description":"How to add internationalization and a language Dropdown to Bootstrap NavBar Menu in Yii2","archived":false,"fork":false,"pushed_at":"2023-12-29T12:06:57.000Z","size":339,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-16T09:41:51.515Z","etag":null,"topics":["bootstrap","bootstrap-theme","bootstrap3","bootstrap4","bootstrap5","i18n","internationalization","menu","menu-navigation","menubar","navbar","navbar-component","navbar-menu","navbars","yii2"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JQL.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-11-27T16:36:54.000Z","updated_at":"2023-12-07T07:10:24.000Z","dependencies_parsed_at":"2023-12-07T08:26:14.978Z","dependency_job_id":"e89ebae8-d056-439b-b1c0-663b2c0b591d","html_url":"https://github.com/JQL/i18ntutorial","commit_stats":null,"previous_names":["jql/i18ntutorial"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JQL%2Fi18ntutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JQL%2Fi18ntutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JQL%2Fi18ntutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JQL%2Fi18ntutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JQL","download_url":"https://codeload.github.com/JQL/i18ntutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248824662,"owners_count":21167343,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bootstrap","bootstrap-theme","bootstrap3","bootstrap4","bootstrap5","i18n","internationalization","menu","menu-navigation","menubar","navbar","navbar-component","navbar-menu","navbars","yii2"],"created_at":"2024-09-24T13:36:34.591Z","updated_at":"2026-01-24T15:07:10.241Z","avatar_url":"https://github.com/JQL.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# How To Add Internationalisation to the NavBar Menu in Yii2\n\nYii comes with internationalisation (i18n) \"out of the box\". There are instructions in the manual as to how to configure Yii to use i18n, but little information all in one place on how to fully integrate it into the bootstrap menu. This document attempts to remedy that.\n\n\u003cimg src=\"https://visualaccounts.co.uk/images/screenshots/Screenshot_i18n_s.png\" /\u003e\n\nThe Github repository also contains the language flags, some country flags, a list of languages codes and their language names and a list of the languages Yii recognises \"out of the box\". A video will be posted on YouTube soon.\n\n---\n\nEnsure that your system is set up to use i18n. From the Yii2 Manual:\n\n\u003e Yii uses the `PHP intl` extension to provide most of its I18N features, such as the date and number formatting of the `yii\\i18n\\Formatter` class and the message formatting using `yii\\i18n\\MessageFormatter`. Both classes provide a fallback mechanism when the intl extension is not installed. However, the fallback implementation only works well for English target language. So it is highly recommended that you install `intl` when I18N is needed.\n\n## Create the required Files\n\nFirst you need to create a configuration file.\n\nDecide where to store it (e.g. in the `./messages/` directory with the name `create_i18n.php`). Create the directory in the project then issue the following command from **Terminal** (Windows: CMD) from the root directory of your project:\n\n```php\n./yii message/config-template ./messages/create_i18n.php\n```\n\nor for more granularity:\n\n```php\n./yii message/config --languages=en-US --sourcePath=@app --messagePath=messages ./messages/create_i18n.php\n```\n\nIn the newly created file, alter (or create) the array of languages to be translated:\n\n```php\n  // array, required, list of language codes that the extracted messages\n  // should be translated to. For example, ['zh-CN', 'de'].\n  'languages' =\u003e [\n    'en-US',\n    'fr',\n    'pt'\n  ],\n```\n\nIf necessary, change the root directory in `create_i18n.php` to point to the messages directory - the default is `messages`. Note, if the above file is in the messages directory (recommended) then don't alter this `'messagePath' =\u003e __DIR__,`. If you alter the directory for `messages` to, say, `/config/` (not a good idea) you can use the following:\n\n```php\n  // Root directory containing message translations.\n  'messagePath' =\u003e __DIR__ . DIRECTORY_SEPARATOR . 'config',\n```\n\nThe created file should look something like this after editing the languages you need:\n\n```php\n\u003c?php\n\nreturn [\n// string, required, root directory of all source files\n  'sourcePath' =\u003e __DIR__ . DIRECTORY_SEPARATOR . '..',\n// array, required, list of language codes that the extracted messages\n// should be translated to. For example, ['zh-CN', 'de'].\n// The languages array is in `/config/languages.php`\n// We need just the Array Keys for this file but, after running `./yii message ./messages/create_i18n.php`\n// Remove the *Default Language folder* (usually `en` or `en-GB`) from the `/messages/` directory\n 'languages' =\u003e array_keys(require(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'languages.php')),\n/*  'languages' =\u003e [\n// to localise a particular language use the language code followed by the dialect in CAPS\n//    'en-GB',  // NOT REQUIRED the sourceLanguage (i.e. the default)\n//    'en' =\u003e 'English', // NOT REQUIRED the sourceLanguage (i.e. the default)\n    'en-US', // USA English\n    'es',\n    'fr',\n    'it',\n    'ja',\n    'pt',\n  ], */\n//    'languages' =\u003e [\n//        'af', 'ar', 'az', 'be', 'bg', 'bs', 'ca', 'cs', 'da', 'de', 'el', 'es', 'et', 'fa', 'fi', 'fr', 'he', 'hi',\n//        'pt-BR', 'ro', 'hr', 'hu', 'hy', 'id', 'it', 'ja', 'ka', 'kk', 'ko', 'kz', 'lt', 'lv', 'ms', 'nb-NO', 'nl',\n//        'pl', 'pt', 'ru', 'sk', 'sl', 'sr', 'sr-Latn', 'sv', 'tg', 'th', 'tr', 'uk', 'uz', 'uz-Cy', 'vi', 'zh-CN',\n//        'zh-TW'\n//    ],\n// string, the name of the function for translating messages.\n// Defaults to 'Yii::t'. This is used as a mark to find the messages to be\n// translated. You may use a string for single function name or an array for\n// multiple function names.\n  'translator' =\u003e ['\\Yii::t', 'Yii::t'],\n  // boolean, whether to sort messages by keys when merging new messages\n// with the existing ones. Defaults to false, which means the new (untranslated)\n// messages will be separated from the old (translated) ones.\n  'sort' =\u003e false,\n  // boolean, whether to remove messages that no longer appear in the source code.\n// Defaults to false, which means these messages will NOT be removed.\n  'removeUnused' =\u003e false,\n  // boolean, whether to mark messages that no longer appear in the source code.\n// Defaults to true, which means each of these messages will be enclosed with a pair of '@@' marks.\n  'markUnused' =\u003e true,\n  // array, list of patterns that specify which files (not directories) should be processed.\n// If empty or not set, all files will be processed.\n// See helpers/FileHelper::findFiles() for pattern matching rules.\n// If a file/directory matches both a pattern in \"only\" and \"except\", it will NOT be processed.\n  'only' =\u003e ['*.php'],\n  // array, list of patterns that specify which files/directories should NOT be processed.\n// If empty or not set, all files/directories will be processed.\n// See helpers/FileHelper::findFiles() for pattern matching rules.\n// If a file/directory matches both a pattern in \"only\" and \"except\", it will NOT be processed.\n  'except' =\u003e [\n    '.*',\n    '/.*',\n    '/messages',\n    '/tests',\n    '/runtime',\n    '/vendor',\n    '/BaseYii.php',\n  ],\n  // 'php' output format is for saving messages to php files.\n  'format' =\u003e 'php',\n  // Root directory containing message translations.\n  'messagePath' =\u003e __DIR__,\n  // boolean, whether the message file should be overwritten with the merged messages\n  'overwrite' =\u003e true,\n  /*\n    // File header used in generated messages files\n    'phpFileHeader' =\u003e '',\n    // PHPDoc used for array of messages with generated messages files\n    'phpDocBlock' =\u003e null,\n   */\n\n  /*\n    // Message categories to ignore\n    'ignoreCategories' =\u003e [\n    'yii',\n    ],\n   */\n\n  /*\n    // 'db' output format is for saving messages to database.\n    'format' =\u003e 'db',\n    // Connection component to use. Optional.\n    'db' =\u003e 'db',\n    // Custom source message table. Optional.\n    // 'sourceMessageTable' =\u003e '{{%source_message}}',\n    // Custom name for translation message table. Optional.\n    // 'messageTable' =\u003e '{{%message}}',\n   */\n\n  /*\n    // 'po' output format is for saving messages to gettext po files.\n    'format' =\u003e 'po',\n    // Root directory containing message translations.\n    'messagePath' =\u003e __DIR__ . DIRECTORY_SEPARATOR . 'messages',\n    // Name of the file that will be used for translations.\n    'catalog' =\u003e 'messages',\n    // boolean, whether the message file should be overwritten with the merged messages\n    'overwrite' =\u003e true,\n   */\n];\n\n```\n\n## Edit the `/config/web.php` file\n\nIn the `web.php` file, below `'id' =\u003e 'basic',` add:\n\n```php\n  'language' =\u003e 'en',\n  'sourceLanguage' =\u003e 'en',\n```\n\nNote: you should always use the `'sourceLanguage' =\u003e 'en'` as it is, usually, easier and cheaper to translate from English into another language. If the `sourceLanguage` is not set it defaults to `'en'`.\n\nAdd the following to the `'components' =\u003e [...]` section:\n\n```php\n    'i18n' =\u003e [\n      'translations' =\u003e [\n        'app*' =\u003e [\n          'class' =\u003e 'yii\\i18n\\PhpMessageSource',  // Using text files (usually faster) for the translations\n          //'basePath' =\u003e '@app/messages',  // Uncomment and change this if your folder is not called 'messages'\n          'sourceLanguage' =\u003e 'en',\n          'fileMap' =\u003e [\n            'app' =\u003e 'app.php',\n            'app/error' =\u003e 'error.php',\n          ],\n          //  Comment out in production version\n          //  'on missingTranslation' =\u003e ['app\\components\\TranslationEventHandler', 'handleMissingTranslation'],\n        ],\n      ],\n    ],\n```\n\n## Edit all the files in the \"views\" folder and any sub folders\n\nNow tell Yii which text you want to translate in your view files. This is done by adding `Yii::t('app', 'text to be translated')` to the code.\n\nFor example, in `/views/layouts/main.php`, change the menu labels like so:\n\n```php\n    'items' =\u003e [\n          //  ['label' =\u003e 'Home', 'url' =\u003e ['/site/index']],\t// Orignal code\n          ['label' =\u003e Yii::t('app', 'Home'), 'url' =\u003e ['/site/index']],\n          ['label' =\u003e Yii::t('app', 'About'), 'url' =\u003e ['/site/about']],\n          ['label' =\u003e Yii::t('app', 'Contact'), 'url' =\u003e ['/site/contact']],\n          Yii::$app-\u003euser-\u003eisGuest ? ['label' =\u003e Yii::t('app', 'Login'), 'url' =\u003e ['/site/login']] : '\u003cli class=\"nav-item\"\u003e'\n            . Html::beginForm(['/site/logout'])\n            . Html::submitButton(\n             // 'Logout (' . Yii::$app-\u003euser-\u003eidentity-\u003eusername . ')', // change this line as well to the following:\n              Yii::t('app', 'Logout ({username})'), ['username' =\u003e Yii::$app-\u003euser-\u003eidentity-\u003eusername]),\n              ['class' =\u003e 'nav-link btn btn-link logout']\n            )\n            . Html::endForm()\n            . '\u003c/li\u003e',\n        ],\n```\n\n## Create the texts to be translated\n\nTo create the translation files, run the following, in **Terminal**, from the root directory of your project:\n\n```php\n./yii message ./messages/create_i18n.php\n```\n\nNow, get the messages translated. For example in the French `/messages/fr/app.php`\n\n```php\n  'Home' =\u003e 'Accueil',\n  'About' =\u003e 'À propos',\n  ...\n```\n\n## Create a Menu Item (Dropdown) to Change the Language\n\nThis takes a number of steps.\n\n### 1. Create an array of languages required\n\nA `key` and a `name` is required for each language.\n\nThe `key` is the ICU language code [ISO 639.1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) in lowercase (with optional Country code [ISO 3166](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes) in uppercase) e.g.\n\n\u003e    French: `fr` or French Canada: `fr-CA`\n\u003e\n\u003e    Portuguese: `pt` or Portuguese Brazil: `pt-BR`\n\nThe `name` is the name of the language *in that language*. e.g. for French: `'Français'`, for Japanese: `'日本の'`. *This is important* as the user may not understand the browser's current language.\n\nIn `/config/params.php` create an array named `languages` with the languages required. For example:\n\n```php\n  /* \t\tList of languages and their codes\n   *\n   * \t\tformat:\n   * \t\t'Language Code' =\u003e 'Language Name',\n   * \t\te.g.\n   * \t\t'fr' =\u003e 'Français',\n   *\n   * \t\tplease use alphabetical order of language code\n   * \t\tUse the language name in the \"user's\" Language\n   *            e.g.\n   *            'ja' =\u003e '日本の',\n   */\n  'languages' =\u003e [\n//    'da' =\u003e 'Danske',\n//    'de' =\u003e 'Deutsche',\n//    'en' =\u003e 'English', // If you want just English\n    'en-GB' =\u003e 'British English',\n    'en-US' =\u003e 'American English',\n    'es' =\u003e 'Español',\n    'fr' =\u003e 'Français',\n    'it' =\u003e 'Italiano',\n//    'ja' =\u003e '日本の',  // Japanese with the word \"Japanese\" in Kanji\n//    'nl' =\u003e 'Nederlandse',\n//    'no' =\u003e 'Norsk',\n//    'pl' =\u003e 'Polski',\n    'pt' =\u003e 'Português',\n//    'ru' =\u003e 'Русский',\n//    'sw' =\u003e 'Svensk',\n//    'zh' =\u003e '中国的',\n  ],\n```\n\n### 2. Create an Action\n\nIn `/controllers/SiteController.php`, the default controller, add an \"Action\" named `actionLanguage()`. This \"Action\" changes the language and sets a cookie so the browser \"remembers\" the language for page requests and return visits to the site.\n\n```php\n  /**\n   * Called by the ajax handler to change the language and\n   * Sets a cookie based on the language selected\n   *\n   */\n  public function actionLanguage()\n  {\n    $lang = Yii::$app-\u003erequest-\u003epost('lang');\n    // If the language \"key\" is not NULL and exists in the languages array in params.php, change the language and set the cookie\n    if ($lang !== NULL \u0026\u0026 array_key_exists($lang, Yii::$app-\u003eparams['languages']))\n    {\n      $expire = time() + (60 * 60 * 24 * 365); //  1 year - alter accordingly\n      Yii::$app-\u003elanguage = $lang;\n      $cookie = new yii\\web\\Cookie([\n        'name' =\u003e 'lang',\n        'value' =\u003e $lang,\n        'expire' =\u003e $expire,\n      ]);\n      Yii::$app-\u003egetResponse()-\u003egetCookies()-\u003eadd($cookie);\n    }\n    Yii::$app-\u003eend();\n  }\n```\n\nRemember to set the method to `POST`. In `behaviors()`, under `actions`, set `'language' =\u003e ['post'],` like so:\n\n```php\n      'verbs' =\u003e [\n        'class' =\u003e VerbFilter::class,\n        'actions' =\u003e [\n          'logout' =\u003e ['post'],\n          'language' =\u003e ['post'],\n        ],\n      ],\n```\n\n### 3. Create a Language Handler\n\nMake sure that the correct language is served for each request.\n\nIn the `/components/ directory`, create a file named: `LanguageHandler.php` and add the following code to it:\n\n```php\n\u003c?php\n\n/**\n * Basic PHP File for LanguageHandler\n *\n * @copyright © 2023, John Lavelle  Created on : 5 Dec 2023, 16:48:17\n *\n * No part of this site may be reproduced without prior permission.\n *\n * Author     : John Lavelle\n * Title      : LanguageHandler\n */\n// Change the Namespace (app, frontend, backend, console etc.) Default is \"app\".\n\nnamespace app\\components;\n\nuse Yii;\n\n//use yii\\helpers\\Html;\n\n/**\n * LanguageHandler //add more information about this file\n *\n * @author John Lavelle\n * @since 1.0 // Update version number\n */\nclass LanguageHandler extends \\yii\\base\\Behavior\n{\n\n  public function events()\n  {\n    return [\\yii\\web\\Application::EVENT_BEFORE_REQUEST =\u003e 'handleBeginRequest'];\n  }\n\n  public function handleBeginRequest($event)\n  {\n    if (\\Yii::$app-\u003egetRequest()-\u003egetCookies()-\u003ehas('lang') \u0026\u0026 array_key_exists(\\Yii::$app-\u003egetRequest()-\u003egetCookies()-\u003egetValue('lang'), \\Yii::$app-\u003eparams['languages']))\n    {\n      \\Yii::$app-\u003elanguage = \\Yii::$app-\u003egetRequest()-\u003egetCookies()-\u003egetValue('lang');\n    }\n    else\n    {\n      //\tUse the browser language\n//      \\Yii::$app-\u003elanguage = str_replace('_', '-', HTML::encode(locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE'])));\n      \\Yii::$app-\u003elanguage = \\Yii::$app-\u003erequest-\u003egetPreferredLanguage();\n    }\n  }\n}\n\n```\n\n### 4. Call `LanguageHandler.php` from `/config/web.php`\n\n\"Call\" the `LanguageHandler.php` file from `/config/web.php` by adding the following to either just above or just below `'params' =\u003e $params,`\n\n```php\n  //\tUpdate the language on selection\n  'as beforeRequest' =\u003e [\n    'class' =\u003e 'app\\components\\LanguageHandler',\n  ],\n```\n\n### 5. Add the Language Menu Item to `/views/layouts/main.php`\n\n`main.php` uses Bootstrap to create the menu. An item (Dropdown) needs to be added to the menu to allow the user to select a language.\n\nAdd `use yii\\helpers\\Url;` to the \"uses\" section of `main.php`.\n\nJust above `echo Nav::widget([...])` add the following code:\n\n```php\n// Get the languages and their keys, also the current route\n      foreach (Yii::$app-\u003eparams['languages'] as $key =\u003e $language)\n      {\n        $items[] = [\n          'label' =\u003e $language, // Language name in it's language - already translated\n          'url' =\u003e Url::to(['site/index']), // Route\n          'linkOptions' =\u003e ['id' =\u003e $key, 'class' =\u003e 'language'], // The language \"key\"\n        ];\n      }\n```\n\nIn the section:\n\n```php\necho Nav::widget([...])`\n```\n\nbetween\n\n```php\n'options' =\u003e ['class' =\u003e 'navbar-nav ms-auto'], // ms-auto aligns the menu right`\n```\n\nand\n\n```php\n'items' =\u003e [...]\n```\n\nadd:\n\n```php\n'encodeLabels' =\u003e false, // Required to enter HTML into the labels\n```\n\nlike so:\n\n```php\n      echo Nav::widget([\n        'options' =\u003e ['class' =\u003e 'navbar-nav ms-auto'], // ms-auto aligns the menu right\n        'encodeLabels' =\u003e false, // Required to enter HTML into the labels\n        'items' =\u003e [\n          ['label' =\u003e Yii::t('app', 'Home'), 'url' =\u003e ['/site/index']],\n        ...\n```\n\nNow add the Dropdown. This can be placed anywhere in `'items' =\u003e [...]`.\n\n```php\n// Dropdown Nav Menu: https://www.yiiframework.com/doc/api/2.0/yii-widgets-menu\n        [\n          'label' =\u003e Yii::t('app', 'Language')),\n          'url' =\u003e ['#'],\n          'options' =\u003e ['class' =\u003e 'language', 'id' =\u003e 'languageTop'],\n          'encodeLabels' =\u003e false, // Optional but required to enter HTML into the labels for images\n          'items' =\u003e $items, // add the languages into the Dropdown\n        ],\n```\n\nThe code in `main.php` for the NavBar should look something like this:\n\n```php\n      NavBar::begin([\n        'brandLabel' =\u003e Yii::$app-\u003ename,  // set in /config/web.php\n        'brandUrl' =\u003e Yii::$app-\u003ehomeUrl,\n        'options' =\u003e ['class' =\u003e 'navbar-expand-md navbar-dark bg-dark fixed-top']\n      ]);\n      // Get the languages and their keys, also the current route\n      foreach (Yii::$app-\u003eparams['languages'] as $key =\u003e $language)\n      {\n        $items[] = [\n          'label' =\u003e $language, // Language name in it's language\n          'url' =\u003e Url::to(['site/index']), // Current route so the page refreshes\n          'linkOptions' =\u003e ['id' =\u003e $key, 'class' =\u003e 'language'], // The language key\n        ];\n      }\n      echo Nav::widget([\n        'options' =\u003e ['class' =\u003e 'navbar-nav ms-auto'], // ms-auto aligns the menu right\n        'encodeLabels' =\u003e false, // Required to enter HTML into the labels\n        'items' =\u003e [\n          ['label' =\u003e Yii::t('app', 'Home'), 'url' =\u003e ['/site/index']],\n          ['label' =\u003e Yii::t('app', 'About'), 'url' =\u003e ['/site/about']],\n          ['label' =\u003e Yii::t('app', 'Contact'), 'url' =\u003e ['/site/contact']],\n          // Dropdown Nav Menu: https://www.yiiframework.com/doc/api/2.0/yii-widgets-menu\n          [\n            'label' =\u003e Yii::t('app', 'Language') ,\n            'url' =\u003e ['#'],\n            'options' =\u003e ['class' =\u003e 'language', 'id' =\u003e 'languageTop'],\n            'encodeLabels' =\u003e false, // Required to enter HTML into the labels\n            'items' =\u003e $items, // add the languages into the Dropdown\n          ],\n          Yii::$app-\u003euser-\u003eisGuest ? ['label' =\u003e Yii::t('app', 'Login'), 'url' =\u003e ['/site/login']] : '\u003cli class=\"nav-item\"\u003e'\n            . Html::beginForm(['/site/logout'])\n            . Html::submitButton(\n//              'Logout (' . Yii::$app-\u003euser-\u003eidentity-\u003eusername . ')',\n              Yii::t('app', 'Logout ({username})', ['username' =\u003e Yii::$app-\u003euser-\u003eidentity-\u003eusername]),\n              ['class' =\u003e 'nav-link btn btn-link logout']\n            )\n            . Html::endForm()\n            . '\u003c/li\u003e',\n        ],\n      ]);\n      NavBar::end();\n```\n\nIf Language flags or images are required next to the language name see *Optional Items* at the end of this document.\n\n### 6. Trigger the Language change with an Ajax call\n\nTo call the Language Action `actionLanguage()` make an Ajax call in a JavaScript file.\n\nCreate a file in `/web/js/` named `language.js`.\n\nAdd the following code to the file:\n\n```JavaScript\n/*\n * Copyright ©2023 JQL all rights reserved.\n * http://www.jql.co.uk\n */\n\n/**\n * Set the language\n *\n * @returns {undefined}\n */\n$(function () {\n  $(document).on('click', '.language', function (event) {\n    event.preventDefault();\n    let lang = $(this).attr('id');  // Get the language key\n    /* if not the top level, set the language and reload the page */\n    if (lang !== 'languageTop') {\n      $.post(document.location.origin + '/site/language', {'lang': lang}, function (data) {\n        location.reload(true);\n      });\n    }\n  });\n});\n```\n\nTo add the JavaScript file to the Assets, alter `/assets/AppAsset.php` in the project directory. In `public $js = []` add `'js/language.js'`, like so:\n\n```php\n     public $js = [\n       'js/language.js',\n     ];\n```\n\n**Internationalisation should now be working on your project.**\n\n\n---\n\n## Optional Items\n\nThe following are optional but may help both you and/or the user.\n\n### 1. Check for Translations\n\nYii can check whether a translation is present for a particular piece of text in a `Yii::t('app', 'text to be translated')` block.\n\nThere are two steps:\n\n**A.** In `/config/web.php uncomment` the following line:\n\n```php\n  //  'on missingTranslation' =\u003e ['app\\components\\TranslationEventHandler', 'handleMissingTranslation'],\n```\n\n**B.** Create a TranslationEventHandler:\n\nIn `/components/` create a file named: `TranslationEventHandler.php` and add the following code to it:\n\n```php\n\n\u003c?php\n\n/**\n * TranslationEventHandler\n *\n * @copyright © 2023, John Lavelle  Created on : 14 Nov 2023, 16:05:32\n *\n *\n * Author     : John Lavelle\n * Title      : TranslationEventHandler\n */\n// Change the Namespace (app, frontend, backend, console etc.) if necessary (default in Yii Basic is \"app\").\n\nnamespace app\\components;\n\nuse yii\\i18n\\MissingTranslationEvent;\n\n/**\n * TranslationEventHandler\n *\n *\n * @author John Lavelle\n * @since 1.0 // Update version number\n */\nclass TranslationEventHandler\n{\n\n  /**\n   * Adds a message to missing translations in Development Environment only\n   *\n   * @param MissingTranslationEvent $event\n   */\n  public static function handleMissingTranslation(MissingTranslationEvent $event)\n  {\n    // Only check in the development environment\n    if (YII_ENV_DEV)\n    {\n      $event-\u003etranslatedMessage = \"@MISSING: {$event-\u003ecategory}.{$event-\u003emessage} FOR LANGUAGE {$event-\u003elanguage} @\";\n    }\n  }\n}\n```\n\nIf there is a missing translation, the text is replaced with a message similar to the following text:\n\n\u003e @MISSING: app.Logout (John) FOR LANGUAGE fr @\n\nHere Yii has found that there is no French translation for:\n\n```php\nYii::t('app', 'Logout ({username})', ['username' =\u003e Yii::$app-\u003euser-\u003eidentity-\u003eusername]),\n```\n\n### 2. Add Language Flags to the Dropdown Menu\n\nThis is very useful and recommended as it aids the User to locate the correct language. There are a number of steps for this.\n\n**a.** Create images of the flags.\n\nThe images should be 25px wide by 15px high. The images **must have** the same name as the language key in the language array in params.php. For example: `fr.png` or `en-US.png`. If the images are not of type \".png\" change the code in part **b.** below to the correct file extension.\n\nPlace the images in a the directory `/web/images/flags/`.\n\n**b.** Alter the code in `/views/layouts/main.php` so that the code for the \"NavBar\" reads as follows:\n\n```php\n\u003cheader id=\"header\"\u003e\n      \u003c?php\n      NavBar::begin([\n        'brandLabel' =\u003e Yii::$app-\u003ename,\n        'brandUrl' =\u003e Yii::$app-\u003ehomeUrl,\n        'options' =\u003e ['class' =\u003e 'navbar-expand-md navbar-dark bg-dark fixed-top']\n      ]);\n      // Get the languages and their keys, also the current route\n      foreach (Yii::$app-\u003eparams['languages'] as $key =\u003e $language)\n      {\n        $items[] = [\n\t// Display the image before the language name\n          'label' =\u003e Html::img('/images/flags/' . $key . '.png', ['alt' =\u003e 'flag ' . $language, 'class' =\u003e 'inline-block align-middle', 'title' =\u003e $language,]) . ' ' . $language, // Language name in it's language\n          'url' =\u003e Url::to(['site/index']), // Route\n          'linkOptions' =\u003e ['id' =\u003e $key, 'class' =\u003e 'language'], // The language key\n        ];\n      }\n      echo Nav::widget([\n        'options' =\u003e ['class' =\u003e 'navbar-nav ms-auto'], // ms-auto aligns the menu right\n        'encodeLabels' =\u003e false, // Required to enter HTML into the labels\n        'items' =\u003e [\n          ['label' =\u003e Yii::t('app', 'Home'), 'url' =\u003e ['/site/index']],\n          ['label' =\u003e Yii::t('app', 'About'), 'url' =\u003e ['/site/about']],\n          ['label' =\u003e Yii::t('app', 'Contact'), 'url' =\u003e ['/site/contact']],\n          // Dropdown Nav Menu: https://www.yiiframework.com/doc/api/2.0/yii-widgets-menu\n          [\n\t  // Display the current language \"flag\" after the Dropdown title (before the caret)\n            'label' =\u003e Yii::t('app', 'Language') . ' ' . Html::img('@web/images/flags/' . Yii::$app-\u003elanguage . '.png', ['class' =\u003e 'inline-block align-middle', 'title' =\u003e Yii::$app-\u003elanguage]),\n            'url' =\u003e ['#'],\n            'options' =\u003e ['class' =\u003e 'language', 'id' =\u003e 'languageTop'],\n            'encodeLabels' =\u003e false, // Required to enter HTML into the labels\n            'items' =\u003e $items, // add the languages into the Dropdown\n          ],\n          Yii::$app-\u003euser-\u003eisGuest ? ['label' =\u003e Yii::t('app', 'Login'), 'url' =\u003e ['/site/login']] : '\u003cli class=\"nav-item\"\u003e'\n            . Html::beginForm(['/site/logout'])\n            . Html::submitButton(\n//              'Logout (' . Yii::$app-\u003euser-\u003eidentity-\u003eusername . ')',\n              Yii::t('app', 'Logout ({username})', ['username' =\u003e Yii::$app-\u003euser-\u003eidentity-\u003eusername]),\n              ['class' =\u003e 'nav-link btn btn-link logout']\n            )\n            . Html::endForm()\n            . '\u003c/li\u003e',\n        ],\n      ]);\n      NavBar::end();\n      ?\u003e\n    \u003c/header\u003e\n```\n\nThat's it! Enjoy...\n\nFor further reading and information see:\n\n[i18ntutorial on Github](https://github.com/JQL/i18ntutorial)\n\n[Yii2 Internationalization Tutorial](https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n)\n\n[PHP intl extensions](https://www.php.net/manual/en/intro.intl.php)\n\n---\n\nIf you use this code, please credit me as follows:\n\n\u003e Internationalization (i18n) Menu code provided by JQL, https://visualaccounts.co.uk ©2023 JQL\n\n---\n\nLicence (BSD-3-Clause Licence)\n\nCopyright Notice\n\n\u003e Internationalization (i18n) Menu code provided by JQL, https://visualaccounts.co.uk ©2023 JQL all rights reserved\n\nRedistribution and use in source and binary forms with or without modification are permitted provided that the following conditions are met:\n\nRedistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n\nRedistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n\nNeither the names of John Lavelle, JQL, Visual Accounts nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.\n\n\"ALL JQL CODE \u0026 SOFTWARE INCLUDING WORLD WIDE WEB PAGES (AND THOSE OF IT'S AUTHORS) ARE SUPPLIED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND. TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE AUTHOR AND PUBLISHER AND THEIR AGENTS SPECIFICALLY DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. WITH RESPECT TO THE CODE, THE AUTHOR AND PUBLISHER AND THEIR AGENTS SHALL HAVE NO LIABILITY WITH RESPECT TO ANY LOSS OR DAMAGE DIRECTLY OR INDIRECTLY ARISING OUT OF THE USE OF THE CODE EVEN IF THE AUTHOR AND/OR PUBLISHER AND THEIR AGENTS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. WITHOUT LIMITING THE FOREGOING, THE AUTHOR AND PUBLISHER AND THEIR AGENTS SHALL NOT BE LIABLE FOR ANY LOSS OF PROFIT, INTERRUPTION OF BUSINESS, DAMAGE TO EQUIPMENT OR DATA, INTERRUPTION OF OPERATIONS OR ANY OTHER COMMERCIAL DAMAGE, INCLUDING BUT NOT LIMITED TO DIRECT, INDIRECT, SPECIAL, INCIDENTAL, CONSEQUENTIAL OR OTHER DAMAGES.\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjql%2Fi18ntutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjql%2Fi18ntutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjql%2Fi18ntutorial/lists"}