Project internationalization has become an indispensable part in the development of the company to achieve internationalization of the requirements, especially applicable to the ANGULARJS to achieve internationalization, words do not say directly to see Operation 1, configuration internationalization 1.1 to introduce JS file We all know that to use the Third-party library files, we must introduce some files, such as style files, JS files, to achieve internationalization needs to introduce angularjs several files
<span style= "White-space:pre" > </span><script type= "Text/javascript" src= "resources/angular/" Angular.min.js "></script>
<script type=" Text/javascript "src=" resources/angular/ Angular-cookies.min.js "></script>
<script type=" Text/javascript "src=" resources/angular/ Angular-translate.min.js "></script>
<script type=" Text/javascript "src=" resources/angular/ Translate-service/storage-cookie.js "></script>
<script type=" Text/javascript "src=" resources/ Angular/translate-service/loader-static-files.js "></script>
Some of the files on translate need to be introduced, the role of introducing Angularjs-cookie is, when the user switches the language, after the refresh, will display the user action language
1.2 Registration Configuration InternationalizationAfter the introduction of JS files, you need to register in the app, in the controller register $translate
Angular.module (' main ', [
"Pascalprecht.translate",
"Ngcookies"
])
. config ([' $translateProvider ' , the function ($translateProvider) {
//json file path
$translateProvider. Usestaticfilesloader ({
prefix: '/ programe/hanlet/statices/data/',
suffix: '. json '
});
The default is to use English
$translateProvider. Preferredlanguage (' en_US ');
Save to Cookie
$translateProvider. Usecookiestorage ();
}] <pre name= "code" class= "HTML" >.controller ("Loginctrl", function ($translate) {
//internationalization
$scope. Setlang = function (Langkey) {
$translate. Use (Langkey);
};}
);
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre code_snippet_id= "1693633" snippet_file_name= "blog_20160523_3_5069568" "Name=" code "class=" JavaScript ">//after registration, in the data file to create the Chinese and English JSON file, the format is as follows: Key value to write
<span style= "font-family:arial, Helvetica, Sans-serif;" >{</span>
"User Guide": "User Guide"
}
<pre name= "code" class= "plain" >{
"User Guide": "Users Guide"
}
To use translate in HTML:
2, switching language eventsTo define a toggle event: HTML:
<a href= "#" ng-click= "Setlang (' en_US ')" >English</a> | <a href= "#" ng-click= "Setlang (' zh_ch ')" > </a>
JS in:
Switch between Chinese and English
$scope. Setlang = function (langkey) {
$translate. Use (Langkey);
};
The above is ANGULARJS implementation internationalization operation