Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

l10n-registry.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. // This var is global because it's shared across webpack bundles
  24. window._oc_l10n_registry_translations = window._oc_l10n_registry_translations || {}
  25. window._oc_l10n_registry_plural_functions = window._oc_l10n_registry_plural_functions || {}
  26. /**
  27. * @param {string} appId the app id
  28. * @param {object} translations the translations list
  29. * @param {Function} pluralFunction the translations list
  30. */
  31. const register = (appId, translations, pluralFunction) => {
  32. window._oc_l10n_registry_translations[appId] = translations
  33. window._oc_l10n_registry_plural_functions[appId] = pluralFunction
  34. }
  35. /**
  36. * @param {string} appId the app id
  37. * @param {object} translations the translations list
  38. * @param {Function} pluralFunction the translations list
  39. */
  40. const extend = (appId, translations, pluralFunction) => {
  41. window._oc_l10n_registry_translations[appId] = Object.assign(
  42. window._oc_l10n_registry_translations[appId],
  43. translations
  44. )
  45. window._oc_l10n_registry_plural_functions[appId] = pluralFunction
  46. }
  47. /**
  48. * @param {string} appId the app id
  49. * @param {object} translations the translations list
  50. * @param {Function} pluralFunction the translations list
  51. */
  52. export const registerAppTranslations = (appId, translations, pluralFunction) => {
  53. if (!hasAppTranslations(appId)) {
  54. register(appId, translations, pluralFunction)
  55. } else {
  56. extend(appId, translations, pluralFunction)
  57. }
  58. }
  59. /**
  60. * @param {string} appId the app id
  61. */
  62. export const unregisterAppTranslations = appId => {
  63. delete window._oc_l10n_registry_translations[appId]
  64. delete window._oc_l10n_registry_plural_functions[appId]
  65. }
  66. /**
  67. * @param {string} appId the app id
  68. * @return {boolean}
  69. */
  70. export const hasAppTranslations = appId => {
  71. return window._oc_l10n_registry_translations[appId] !== undefined
  72. && window._oc_l10n_registry_plural_functions[appId] !== undefined
  73. }
  74. /**
  75. * @param {string} appId the app id
  76. * @return {object}
  77. */
  78. export const getAppTranslations = appId => {
  79. return {
  80. translations: window._oc_l10n_registry_translations[appId] || {},
  81. pluralFunction: window._oc_l10n_registry_plural_functions[appId],
  82. }
  83. }