A simple JS library to support multiple language (multi-lang) feature or translations on sites
- jQuery
Simply download the dictionary-multilang file dictionary.js
into your website folder and you are ready to go!
- Import dictionary-multilang-js package
<script src="path/to/dictionary.js"></script>
- Initialise language packs
var dictionary = new Dictionary();
dictionary.init({
en: 'path/to/lang.english.json',
bm: 'path/to/lang.bahasa.json',
th: 'path/to/lang.thai.json,
default: 'en'
});
Read more on the JSON tree structure in the note section
- Initialise language toggle buttons
dictionary.initButtons({
en: 'btn-en',
bm: 'btn-bm',
th: 'btn-th'
});
- Tag the elements that you want translated, with
data-dictionary="text.location.in.JSON.tree"
<h1 data-dictionary="body.some_heading.text"></h1>
Read more on the data-dictionary
value, in the note section
- Add the language buttons
<a class="btn-en">EN</a> | <a class="btn-bm">BM</a> | <a class="btn-th">TH</a>
That's it! The rest will magically happen when you simply toggle the languages.
Have fun!
The active language button will automatically have the class active-dictionary
. So you can simply style the active language by using the CSS class selector .active-dictionary
a.active-dictionary {
text-decoration: underline;
}
placeholder-dictionary
Add it to translate placeholder texts in form inputs
<input data-dictionary="form.placeholders.name" placeholder-dictionary type="text" name="name">
- If you need to add additional actions after the user clicks on the toggle language buttons, you may use
addButtonActions
and define a function into it.
Note: Make sure you use addButtonActions
before initButtons
to ensure the actions are binded to the language buttons.
dictionary.addButtonActions = function() {
console.log('execute me!);
}
dictionary.initButtons({
en: 'btn-en',
bm: 'btn-bm',
th: 'btn-th',
});
- The
JSON
file will act as language packs that have different translations of the same text. - It loads up the language packs (that contains the text strings) from the supplied
JSON
file. - Initialise the elements with
data-dictionary
attribute with the text from the default language specified. - Change the
data-dictionary
elements'innerHTML
, based on the selected language, by toggling the buttons.
The value for the data-dictionary
attribute is the location of the text string inside the JSON
file.
Example:
lang.english.json
{
"body": {
"some_heading" : "your header text",
"some_other_heading": "anyone\'s header text"
}
}
lang.thai.json
{
"body": {
"some_heading" : "ข้อความส่วนหัวของคุณ",
"some_other_heading": "ข้อความส่วนหัวของทุกคน"
}
}
your_page.html
<!-- The value within the <h1> tag will be 'your header text' or 'ข้อความส่วนหัวของคุณ' -->
<h1 data-dictionary="body.some_heading"></h1>
- Create your feature branch (
git checkout -b new-feature
) - Commit your changes (
git commit -am 'Some cool reflection'
) - Push to the branch (
git push origin new-feature
) - Create new Pull Request
MIT