A lightweight internationalization (I18n) plug-in based on jQuery.
- Support to set the default language
- Support switching languages
- Support for using JSON files to store translation content
You can customize different language versions of the JSON file according to the user, according to the need to render the language on the web page, to achieve internationalization.
The installation
You can go to github.com/ZOMAKE/jque… Download the latest version
Start with jQuery files:
<script src="jquery.js"></script>Copy the code
Jquery.i18n.js = jquery.i18n.js
<script src="jquery.i18n.js"></script>Copy the code
Create a new i18N folder in the project and place i18n_XX. json language files of different language versions in this folder. (You can also customize the beginning and end of file names such as baiducnlang.js, which in the following example starts with i18n_.)
Add i18N attributes to DOM structures where internationalization is required (value in I18N is the key in the language file, which can be customized) :
<div i18n="i18n.test">multi-language</div>
<div i18n="i18n.test2">internationalization</div>Copy the code
Then you just need to add content to the language file. For example, the Chinese language file is named i18n_cn.json, and the English language file is named i18n_en.json:
I18n_cn. Json:
{"i18n.test": "multilingual ", "i18n.test2":" internationalization "}
Copy the code
The corresponding translation file i18n_en.json:
{
"i18n.test": "multi-language",
"i18n.test2": "internationalization"
}
Copy the code
Finally, execute the following method in the script to initialize the plug-in.
$("[i18n]").i18n({
defaultLang: "en",
filePath: "/i18n/",
filePrefix: "i18n_",
fileSuffix: "",
forever: true.callback: function() {}});Copy the code
configuration
defaultLang: defaultLang,Copy the code
The default language name. The plugin will automatically splice filePrefix+defaultLang+fileSuffix together as the language file name.
filePath
filePath: "/i18n/".Copy the code
This parameter specifies the location of the language file folder in the project.
filePrefix: "i18n_".Copy the code
This parameter specifies the prefix of the language file’s name.
fileSuffix
fileSuffix: "".Copy the code
This parameter specifies the suffix of the language file’s name.
callback:function() {//do something
}Copy the code
i18nOnly
By default, HTML elements placeholder, value, and HTML are translated and replaced together. If you want to replace only one of them, you can declare the i18nOnly attribute in the HTML tag.
<input i18n="i18n.test" i18n-only="placeholder" placeholder="multilingual"></input>Copy the code
- T-baby
- NiroDu