]> git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blob - sources/core/lang.js
Validation initiale
[perso/Immae/Projets/packagist/ludivine-ckeditor-component.git] / sources / core / lang.js
1 /**
2 * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
4 */
5
6 ( function() {
7 /**
8 * Stores language-related functions.
9 *
10 * @class
11 * @singleton
12 */
13 CKEDITOR.lang = {
14 /**
15 * The list of languages available in the editor core.
16 *
17 * alert( CKEDITOR.lang.languages.en ); // 1
18 */
19 languages: {
20 af: 1, ar: 1, az: 1, bg: 1, bn: 1, bs: 1, ca: 1, cs: 1, cy: 1, da: 1, de: 1, 'de-ch': 1, el: 1,
21 'en-au': 1, 'en-ca': 1, 'en-gb': 1, en: 1, eo: 1, es: 1, et: 1, eu: 1, fa: 1, fi: 1, fo: 1,
22 'fr-ca': 1, fr: 1, gl: 1, gu: 1, he: 1, hi: 1, hr: 1, hu: 1, id: 1, is: 1, it: 1, ja: 1, ka: 1,
23 km: 1, ko: 1, ku: 1, lt: 1, lv: 1, mk: 1, mn: 1, ms: 1, nb: 1, nl: 1, no: 1, oc: 1, pl: 1, 'pt-br': 1,
24 pt: 1, ro: 1, ru: 1, si: 1, sk: 1, sl: 1, sq: 1, 'sr-latn': 1, sr: 1, sv: 1, th: 1, tr: 1, tt: 1, ug: 1,
25 uk: 1, vi: 1, 'zh-cn': 1, zh: 1
26 },
27
28 /**
29 * The list of languages that are written Right-To-Left (RTL) and are supported by the editor.
30 */
31 rtl: { ar: 1, fa: 1, he: 1, ku: 1, ug: 1 },
32
33 /**
34 * Loads a specific language file, or auto detects it. A callback is
35 * then called when the file gets loaded.
36 *
37 * @param {String} languageCode The code of the language file to be
38 * loaded. If null or empty, autodetection will be performed. The
39 * same happens if the language is not supported.
40 * @param {String} defaultLanguage The language to be used if
41 * `languageCode` is not supported or if the autodetection fails.
42 * @param {Function} callback A function to be called once the
43 * language file is loaded. Two parameters are passed to this
44 * function: the language code and the loaded language entries.
45 */
46 load: function( languageCode, defaultLanguage, callback ) {
47 // If no languageCode - fallback to browser or default.
48 // If languageCode - fallback to no-localized version or default.
49 if ( !languageCode || !CKEDITOR.lang.languages[ languageCode ] )
50 languageCode = this.detect( defaultLanguage, languageCode );
51
52 var that = this,
53 loadedCallback = function() {
54 that[ languageCode ].dir = that.rtl[ languageCode ] ? 'rtl' : 'ltr';
55 callback( languageCode, that[ languageCode ] );
56 };
57
58 if ( !this[ languageCode ] )
59 CKEDITOR.scriptLoader.load( CKEDITOR.getUrl( 'lang/' + languageCode + '.js' ), loadedCallback, this );
60 else
61 loadedCallback();
62 },
63
64 /**
65 * Returns the language that best fits the user language. For example,
66 * suppose that the user language is "pt-br". If this language is
67 * supported by the editor, it is returned. Otherwise, if only "pt" is
68 * supported, it is returned instead. If none of the previous are
69 * supported, a default language is then returned.
70 *
71 * alert( CKEDITOR.lang.detect( 'en' ) ); // e.g., in a German browser: 'de'
72 *
73 * @param {String} defaultLanguage The default language to be returned
74 * if the user language is not supported.
75 * @param {String} [probeLanguage] A language code to try to use,
76 * instead of the browser-based autodetection.
77 * @returns {String} The detected language code.
78 */
79 detect: function( defaultLanguage, probeLanguage ) {
80 var languages = this.languages;
81 probeLanguage = probeLanguage || navigator.userLanguage || navigator.language || defaultLanguage;
82
83 var parts = probeLanguage.toLowerCase().match( /([a-z]+)(?:-([a-z]+))?/ ),
84 lang = parts[ 1 ],
85 locale = parts[ 2 ];
86
87 if ( languages[ lang + '-' + locale ] )
88 lang = lang + '-' + locale;
89 else if ( !languages[ lang ] )
90 lang = null;
91
92 CKEDITOR.lang.detect = lang ?
93 function() {
94 return lang;
95 } : function( defaultLanguage ) {
96 return defaultLanguage;
97 };
98
99 return lang || defaultLanguage;
100 }
101 };
102
103 } )();