lang.js 3.7 KB

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