Revision #9 has been created by JQL on Dec 5, 2023, 2:57:36 PM with the memo:
Edited title and fixed some typos
« previous (#8) next (#10) »
Changes
Title
unchanged
How to add a DropDown Language Picker to the Menu
Category
unchanged
How-tos
Yii version
unchanged
2.0
Tags
unchanged
i18n,AJAX,bootstrap,internationalization,bootstrap menu
Content
changed
# How To Add Internationalisation to the
NavBar Menu in Yii2
Basic (Bootstrap 5)
Yii 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.[...]
'Home' => 'Accueil',
'About' => 'À propos',
...
```
## Create a Menu Item (Dropdown) to Change the Language[...]
A `key` and a `name` is required for each language.
The `key` is the ICU language code in lowercase (with optional [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
_code
s) in uppercase) e.g.
> French: `fr` or French Canada: `fr-CA`[...]
> Portuguese: `pt` or Portuguese Brazil: `pt-BR`
The `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.
In `/config/params.php` create an array named `languages` with the languages required. For example:[...]
```
If countryLanguage flags or images are required next to the language name see *Optional Items* at the end of this document.
### 6. Trigger the Language change with an Ajax call[...]
To call the Language Action `actionLanguage()` make an Ajax call in a JavaScript file.
Create a file in `/public_htmlweb/js/` named `language.js`.
Add the following code to the file:[...]
```
**Internationalisation should now be working on your project.
**[...]
```
### 2. Add CountryLanguage Flags to the Dropdown Menu
This is very useful and recommended as it aids the User to locate the correct language. There are a number of steps for this.[...]
Copyright Notice
> Internationalization (i18n) Menu code provided by JQL, https://visualaccounts.co.uk ©2023 JQL all rights reserved
Redistribution and use in source and binary forms with or without modification are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.[...]