Importation initiale du code de CKEditor 3.6.1.
[ckeditor.git] / _source / core / lang.js
1 /*
2 Copyright (c) 2003-2011, 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 'lt' : 1,
60 'lv' : 1,
61 'mn' : 1,
62 'ms' : 1,
63 'nb' : 1,
64 'nl' : 1,
65 'no' : 1,
66 'pl' : 1,
67 'pt-br' : 1,
68 'pt' : 1,
69 'ro' : 1,
70 'ru' : 1,
71 'sk' : 1,
72 'sl' : 1,
73 'sr-latn' : 1,
74 'sr' : 1,
75 'sv' : 1,
76 'th' : 1,
77 'tr' : 1,
78 'uk' : 1,
79 'vi' : 1,
80 'zh-cn' : 1,
81 'zh' : 1
82 },
83
84 /**
85 * Loads a specific language file, or auto detect it. A callback is
86 * then called when the file gets loaded.
87 * @param {String} languageCode The code of the language file to be
88 * loaded. If null or empty, autodetection will be performed. The
89 * same happens if the language is not supported.
90 * @param {String} defaultLanguage The language to be used if
91 * languageCode is not supported or if the autodetection fails.
92 * @param {Function} callback A function to be called once the
93 * language file is loaded. Two parameters are passed to this
94 * function: the language code and the loaded language entries.
95 * @example
96 */
97 load : function( languageCode, defaultLanguage, callback )
98 {
99 // If no languageCode - fallback to browser or default.
100 // If languageCode - fallback to no-localized version or default.
101 if ( !languageCode || !CKEDITOR.lang.languages[ languageCode ] )
102 languageCode = this.detect( defaultLanguage, languageCode );
103
104 if ( !this[ languageCode ] )
105 {
106 CKEDITOR.scriptLoader.load( CKEDITOR.getUrl(
107 '_source/' + // @Packager.RemoveLine
108 'lang/' + languageCode + '.js' ),
109 function()
110 {
111 callback( languageCode, this[ languageCode ] );
112 }
113 , this );
114 }
115 else
116 callback( languageCode, this[ languageCode ] );
117 },
118
119 /**
120 * Returns the language that best fit the user language. For example,
121 * suppose that the user language is "pt-br". If this language is
122 * supported by the editor, it is returned. Otherwise, if only "pt" is
123 * supported, it is returned instead. If none of the previous are
124 * supported, a default language is then returned.
125 * @param {String} defaultLanguage The default language to be returned
126 * if the user language is not supported.
127 * @param {String} [probeLanguage] A language code to try to use,
128 * instead of the browser based autodetection.
129 * @returns {String} The detected language code.
130 * @example
131 * alert( CKEDITOR.lang.detect( 'en' ) ); // e.g., in a German browser: "de"
132 */
133 detect : function( defaultLanguage, probeLanguage )
134 {
135 var languages = this.languages;
136 probeLanguage = probeLanguage || navigator.userLanguage || navigator.language;
137
138 var parts = probeLanguage
139 .toLowerCase()
140 .match( /([a-z]+)(?:-([a-z]+))?/ ),
141 lang = parts[1],
142 locale = parts[2];
143
144 if ( languages[ lang + '-' + locale ] )
145 lang = lang + '-' + locale;
146 else if ( !languages[ lang ] )
147 lang = null;
148
149 CKEDITOR.lang.detect = lang ?
150 function() { return lang; } :
151 function( defaultLanguage ) { return defaultLanguage; };
152
153 return lang || defaultLanguage;
154 }
155 };
156
157 })();