Angularjs realize internationalization __js

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.