1 /*
  2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html or http://ckeditor.com/license
  4 */
  5 
  6 (function()
  7 {
  8 	var loadedLangs = {};
  9 
 10 	/**
 11 	 * @namespace Holds language related functions.
 12 	 */
 13 	CKEDITOR.lang =
 14 	{
 15 		/**
 16 		 * The list of languages available in the editor core.
 17 		 * @type Object
 18 		 * @example
 19 		 * alert( CKEDITOR.lang.en );  // "true"
 20 		 */
 21 		languages :
 22 		{
 23 			'af'	: 1,
 24 			'ar'	: 1,
 25 			'bg'	: 1,
 26 			'bn'	: 1,
 27 			'bs'	: 1,
 28 			'ca'	: 1,
 29 			'cs'	: 1,
 30 			'cy'	: 1,
 31 			'da'	: 1,
 32 			'de'	: 1,
 33 			'el'	: 1,
 34 			'en-au'	: 1,
 35 			'en-ca'	: 1,
 36 			'en-gb'	: 1,
 37 			'en'	: 1,
 38 			'eo'	: 1,
 39 			'es'	: 1,
 40 			'et'	: 1,
 41 			'eu'	: 1,
 42 			'fa'	: 1,
 43 			'fi'	: 1,
 44 			'fo'	: 1,
 45 			'fr-ca'	: 1,
 46 			'fr'	: 1,
 47 			'gl'	: 1,
 48 			'gu'	: 1,
 49 			'he'	: 1,
 50 			'hi'	: 1,
 51 			'hr'	: 1,
 52 			'hu'	: 1,
 53 			'is'	: 1,
 54 			'it'	: 1,
 55 			'ja'	: 1,
 56 			'ka'	: 1,
 57 			'km'	: 1,
 58 			'ko'	: 1,
 59 			'ku'	: 1,
 60 			'lt'	: 1,
 61 			'lv'	: 1,
 62 			'mn'	: 1,
 63 			'ms'	: 1,
 64 			'nb'	: 1,
 65 			'nl'	: 1,
 66 			'no'	: 1,
 67 			'pl'	: 1,
 68 			'pt-br'	: 1,
 69 			'pt'	: 1,
 70 			'ro'	: 1,
 71 			'ru'	: 1,
 72 			'sk'	: 1,
 73 			'sl'	: 1,
 74 			'sr-latn'	: 1,
 75 			'sr'	: 1,
 76 			'sv'	: 1,
 77 			'th'	: 1,
 78 			'tr'	: 1,
 79 			'ug'	: 1,
 80 			'uk'	: 1,
 81 			'vi'	: 1,
 82 			'zh-cn'	: 1,
 83 			'zh'	: 1
 84 		},
 85 
 86 		/**
 87 		 * Loads a specific language file, or auto detect it. A callback is
 88 		 * then called when the file gets loaded.
 89 		 * @param {String} languageCode The code of the language file to be
 90 		 *		loaded. If null or empty, autodetection will be performed. The
 91 		 *		same happens if the language is not supported.
 92 		 * @param {String} defaultLanguage The language to be used if
 93 		 *		languageCode is not supported or if the autodetection fails.
 94 		 * @param {Function} callback A function to be called once the
 95 		 *		language file is loaded. Two parameters are passed to this
 96 		 *		function: the language code and the loaded language entries.
 97 		 * @example
 98 		 */
 99 		load : function( languageCode, defaultLanguage, callback )
100 		{
101 			// If no languageCode - fallback to browser or default.
102 			// If languageCode - fallback to no-localized version or default.
103 			if ( !languageCode || !CKEDITOR.lang.languages[ languageCode ] )
104 				languageCode = this.detect( defaultLanguage, languageCode );
105 
106 			if ( !this[ languageCode ] )
107 			{
108 				CKEDITOR.scriptLoader.load( CKEDITOR.getUrl(
109 					'_source/' +	// @Packager.RemoveLine
110 					'lang/' + languageCode + '.js' ),
111 					function()
112 						{
113 							callback( languageCode, this[ languageCode ] );
114 						}
115 						, this );
116 			}
117 			else
118 				callback( languageCode, this[ languageCode ] );
119 		},
120 
121 		/**
122 		 * Returns the language that best fit the user language. For example,
123 		 * suppose that the user language is "pt-br". If this language is
124 		 * supported by the editor, it is returned. Otherwise, if only "pt" is
125 		 * supported, it is returned instead. If none of the previous are
126 		 * supported, a default language is then returned.
127 		 * @param {String} defaultLanguage The default language to be returned
128 		 *		if the user language is not supported.
129 		 * @param {String} [probeLanguage] A language code to try to use,
130 		 *		instead of the browser based autodetection.
131 		 * @returns {String} The detected language code.
132 		 * @example
133 		 * alert( CKEDITOR.lang.detect( 'en' ) );  // e.g., in a German browser: "de"
134 		 */
135 		detect : function( defaultLanguage, probeLanguage )
136 		{
137 			var languages = this.languages;
138 			probeLanguage = probeLanguage || navigator.userLanguage || navigator.language || defaultLanguage;
139 
140 			var parts = probeLanguage
141 					.toLowerCase()
142 					.match( /([a-z]+)(?:-([a-z]+))?/ ),
143 				lang = parts[1],
144 				locale = parts[2];
145 
146 			if ( languages[ lang + '-' + locale ] )
147 				lang = lang + '-' + locale;
148 			else if ( !languages[ lang ] )
149 				lang = null;
150 
151 			CKEDITOR.lang.detect = lang ?
152 				function() { return lang; } :
153 				function( defaultLanguage ) { return defaultLanguage; };
154 
155 			return lang || defaultLanguage;
156 		}
157 	};
158 
159 })();
160