dictionariesManager.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { isObject, deepClone } from '../helpers/object';
  2. import { extendNotExistingKeys } from './utils';
  3. import staticRegister from '../utils/staticRegister';
  4. import DEFAULT_DICTIONARY from './languages/en-US';
  5. const DEFAULT_LANGUAGE_CODE = DEFAULT_DICTIONARY.languageCode;
  6. const {
  7. register: registerGloballyLanguageDictionary,
  8. getItem: getGlobalLanguageDictionary,
  9. hasItem: hasGlobalLanguageDictionary,
  10. getValues: getGlobalLanguagesDictionaries
  11. } = staticRegister('languagesDictionaries');
  12. /**
  13. * Register language dictionary for specific language code.
  14. *
  15. * @param {String|Object} languageCodeOrDictionary Language code for specific language i.e. 'en-US', 'pt-BR', 'de-DE' or object representing dictionary.
  16. * @param {Object} dictionary Dictionary for specific language (optional if first parameter has already dictionary).
  17. */
  18. function registerLanguage(languageCodeOrDictionary, dictionary) {
  19. let languageCode = languageCodeOrDictionary;
  20. let dictionaryObject = dictionary;
  21. // Dictionary passed as first argument.
  22. if (isObject(languageCodeOrDictionary)) {
  23. dictionaryObject = languageCodeOrDictionary;
  24. languageCode = dictionaryObject.languageCode;
  25. }
  26. extendLanguageDictionary(languageCode, dictionaryObject);
  27. registerGloballyLanguageDictionary(languageCode, deepClone(dictionaryObject));
  28. // We do not allow user to work with dictionary by reference, it can cause lot of bugs.
  29. return deepClone(dictionaryObject);
  30. }
  31. /**
  32. * Get language dictionary for specific language code.
  33. *
  34. * @param {String} languageCode Language code.
  35. * @returns {Object} Object with constants representing identifiers for translation (as keys) and corresponding translation phrases (as values).
  36. */
  37. function getLanguage(languageCode) {
  38. if (!hasLanguage(languageCode)) {
  39. return null;
  40. }
  41. return deepClone(getGlobalLanguageDictionary(languageCode));
  42. }
  43. /**
  44. *
  45. * Get if language with specified language code was registered.
  46. *
  47. * @param {String} languageCode Language code for specific language i.e. 'en-US', 'pt-BR', 'de-DE'.
  48. * @returns {Boolean}
  49. */
  50. function hasLanguage(languageCode) {
  51. return hasGlobalLanguageDictionary(languageCode);
  52. }
  53. /**
  54. * Get default language dictionary.
  55. *
  56. * @returns {Object} Object with constants representing identifiers for translation (as keys) and corresponding translation phrases (as values).
  57. */
  58. function getDefaultLanguage() {
  59. return DEFAULT_DICTIONARY;
  60. }
  61. /**
  62. * Extend handled dictionary by default language dictionary. As result, if any dictionary key isn't defined for specific language, it will be filled with default language value ("dictionary gaps" are supplemented).
  63. *
  64. * @private
  65. * @param {String} languageCode Language code.
  66. * @param {Object} dictionary Dictionary which is extended.
  67. */
  68. function extendLanguageDictionary(languageCode, dictionary) {
  69. if (languageCode !== DEFAULT_LANGUAGE_CODE) {
  70. extendNotExistingKeys(dictionary, getGlobalLanguageDictionary(DEFAULT_LANGUAGE_CODE));
  71. }
  72. }
  73. /**
  74. * Get registered language dictionaries.
  75. *
  76. * @returns {Array}
  77. */
  78. function getLanguages() {
  79. return getGlobalLanguagesDictionaries();
  80. }
  81. export {
  82. registerLanguage as registerLanguageDictionary,
  83. getLanguage as getLanguageDictionary,
  84. hasLanguage as hasLanguageDictionary,
  85. getDefaultLanguage as getDefaultLanguageDictionary,
  86. getLanguages as getLanguagesDictionaries,
  87. DEFAULT_LANGUAGE_CODE
  88. };
  89. /**
  90. * Automatically registers default dictionary.
  91. */
  92. registerLanguage(DEFAULT_DICTIONARY);