1 /*
  2 Copyright (c) 2003-2009, 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 	CKEDITOR.lang =
 11 	{
 12 		/**
 13 		 * The list of languages available in the editor core.
 14 		 * @type Object
 15 		 * @example
 16 		 * alert( CKEDITOR.lang.en );  // "true"
 17 		 */
 18 		languages :
 19 		{
 20 			'en'	: 1
 21 		},
 22
 23 		/**
 24 		 * Loads a specific language file, or auto detect it. A callback is
 25 		 * then called when the file gets loaded.
 26 		 * @param {String} languageCode The code of the language file to be
 27 		 *		loaded. If "autoDetect" is set to true, this language will be
 28 		 *		used as the default one, if the detect language is not
 29 		 *		available in the core.
 30 		 * @param {Boolean} autoDetect Indicates that the function must try to
 31 		 *		detect the user language and load it instead.
 32 		 * @param {Function} callback The function to be called once the
 33 		 *		language file is loaded. Two parameters are passed to this
 34 		 *		function: the language code and the loaded language entries.
 35 		 * @example
 36 		 */
 37 		load : function( languageCode, autoDetect, callback )
 38 		{
 39 			if ( autoDetect )
 40 				languageCode = this.detect( languageCode );
 41
 42 			if ( !this[ languageCode ] )
 43 			{
 44 				CKEDITOR.scriptLoader.load( CKEDITOR.getUrl(
 46 					'lang/' + languageCode + '.js' ),
 47 					function()
 48 						{
 49 							callback( languageCode, this[ languageCode ] );
 50 						}
 51 						, this );
 52 			}
 53 			else
 54 				callback( languageCode, this[ languageCode ] );
 55 		},
 56
 57 		/**
 58 		 * Returns the language that best fit the user language. For example,
 59 		 * suppose that the user language is "pt-br". If this language is
 60 		 * supported by the editor, it is returned. Otherwise, if only "pt" is
 61 		 * supported, it is returned instead. If none of the previous are
 62 		 * supported, a default language is then returned.
 63 		 * @param {String} defaultLanguage The default language to be returned
 64 		 *		if the user language is not supported.
 65 		 * @returns {String} The detected language code.
 66 		 * @example
 67 		 * alert( CKEDITOR.lang.detect( 'en' ) );  // e.g., in a German browser: "de"
 68 		 */
 69 		detect : function( defaultLanguage )
 70 		{
 71 			var languages = this.languages;
 72
 73 			var parts = ( navigator.userLanguage || navigator.language )
 74 					.toLowerCase()
 75 					.match( /([a-z]+)(?:-([a-z]+))?/ ),
 76 				lang = parts[1],
 77 				locale = parts[2];
 78
 79 			if ( languages[ lang + '-' + locale ] )
 80 				lang = lang + '-' + locale;
 81 			else if ( !languages[ lang ] )
 82 				lang = null;
 83
 84 			CKEDITOR.lang.detect = lang ?
 85 				function() { return lang; } :
 86 				function( defaultLanguage ) { return defaultLanguage; };
 87
 88 			return lang || defaultLanguage;
 89 		}
 90 	};
 91
 92 })();
 93