You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

l10n.js 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  3. * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  4. * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license GNU AGPL version 3 or any later version
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as
  18. * published by the Free Software Foundation, either version 3 of the
  19. * License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. */
  30. import _ from 'underscore'
  31. import $ from 'jquery'
  32. import DOMPurify from 'dompurify'
  33. import Handlebars from 'handlebars'
  34. import identity from 'lodash/fp/identity'
  35. import escapeHTML from 'escape-html'
  36. import { generateFilePath } from '@nextcloud/router'
  37. import OC from './index'
  38. import {
  39. getAppTranslations,
  40. hasAppTranslations,
  41. registerAppTranslations,
  42. unregisterAppTranslations,
  43. } from './l10n-registry'
  44. /**
  45. * L10N namespace with localization functions.
  46. *
  47. * @namespace OC.L10n
  48. */
  49. const L10n = {
  50. /**
  51. * Load an app's translation bundle if not loaded already.
  52. *
  53. * @param {string} appName name of the app
  54. * @param {Function} callback callback to be called when
  55. * the translations are loaded
  56. * @return {Promise} promise
  57. */
  58. load(appName, callback) {
  59. // already available ?
  60. if (hasAppTranslations(appName) || OC.getLocale() === 'en') {
  61. const deferred = $.Deferred()
  62. const promise = deferred.promise()
  63. promise.then(callback)
  64. deferred.resolve()
  65. return promise
  66. }
  67. const self = this
  68. const url = generateFilePath(appName, 'l10n', OC.getLocale() + '.json')
  69. // load JSON translation bundle per AJAX
  70. return $.get(url)
  71. .then(
  72. function(result) {
  73. if (result.translations) {
  74. self.register(appName, result.translations, result.pluralForm)
  75. }
  76. })
  77. .then(callback)
  78. },
  79. /**
  80. * Register an app's translation bundle.
  81. *
  82. * @param {string} appName name of the app
  83. * @param {object<string, string>} bundle bundle
  84. */
  85. register(appName, bundle) {
  86. registerAppTranslations(appName, bundle, this._getPlural)
  87. },
  88. /**
  89. * @private
  90. */
  91. _unregister: unregisterAppTranslations,
  92. /**
  93. * Translate a string
  94. *
  95. * @param {string} app the id of the app for which to translate the string
  96. * @param {string} text the string to translate
  97. * @param {object} [vars] map of placeholder key to value
  98. * @param {number} [count] number to replace %n with
  99. * @param {Array} [options] options array
  100. * @param {boolean} [options.escape=true] enable/disable auto escape of placeholders (by default enabled)
  101. * @param {boolean} [options.sanitize=true] enable/disable sanitization (by default enabled)
  102. * @return {string}
  103. */
  104. translate(app, text, vars, count, options) {
  105. const defaultOptions = {
  106. escape: true,
  107. sanitize: true,
  108. }
  109. const allOptions = options || {}
  110. _.defaults(allOptions, defaultOptions)
  111. const optSanitize = allOptions.sanitize ? DOMPurify.sanitize : identity
  112. const optEscape = allOptions.escape ? escapeHTML : identity
  113. // TODO: cache this function to avoid inline recreation
  114. // of the same function over and over again in case
  115. // translate() is used in a loop
  116. const _build = function(text, vars, count) {
  117. return text.replace(/%n/g, count).replace(/{([^{}]*)}/g,
  118. function(a, b) {
  119. const r = vars[b]
  120. if (typeof r === 'string' || typeof r === 'number') {
  121. return optSanitize(optEscape(r))
  122. } else {
  123. return optSanitize(a)
  124. }
  125. }
  126. )
  127. }
  128. let translation = text
  129. const bundle = getAppTranslations(app)
  130. const value = bundle.translations[text]
  131. if (typeof (value) !== 'undefined') {
  132. translation = value
  133. }
  134. if (typeof vars === 'object' || count !== undefined) {
  135. return optSanitize(_build(translation, vars, count))
  136. } else {
  137. return optSanitize(translation)
  138. }
  139. },
  140. /**
  141. * Translate a plural string
  142. *
  143. * @param {string} app the id of the app for which to translate the string
  144. * @param {string} textSingular the string to translate for exactly one object
  145. * @param {string} textPlural the string to translate for n objects
  146. * @param {number} count number to determine whether to use singular or plural
  147. * @param {object} [vars] map of placeholder key to value
  148. * @param {Array} [options] options array
  149. * @param {boolean} [options.escape=true] enable/disable auto escape of placeholders (by default enabled)
  150. * @return {string} Translated string
  151. */
  152. translatePlural(app, textSingular, textPlural, count, vars, options) {
  153. const identifier = '_' + textSingular + '_::_' + textPlural + '_'
  154. const bundle = getAppTranslations(app)
  155. const value = bundle.translations[identifier]
  156. if (typeof (value) !== 'undefined') {
  157. const translation = value
  158. if ($.isArray(translation)) {
  159. const plural = bundle.pluralFunction(count)
  160. return this.translate(app, translation[plural], vars, count, options)
  161. }
  162. }
  163. if (count === 1) {
  164. return this.translate(app, textSingular, vars, count, options)
  165. } else {
  166. return this.translate(app, textPlural, vars, count, options)
  167. }
  168. },
  169. /**
  170. * The plural function taken from symfony
  171. *
  172. * @param {number} number the number of elements
  173. * @return {number}
  174. * @private
  175. */
  176. _getPlural(number) {
  177. let language = OC.getLanguage()
  178. if (language === 'pt-BR') {
  179. // temporary set a locale for brazilian
  180. language = 'xbr'
  181. }
  182. if (typeof language === 'undefined' || language === '') {
  183. return (number === 1) ? 0 : 1
  184. }
  185. if (language.length > 3) {
  186. language = language.substring(0, language.lastIndexOf('-'))
  187. }
  188. /*
  189. * The plural rules are derived from code of the Zend Framework (2010-09-25),
  190. * which is subject to the new BSD license (http://framework.zend.com/license/new-bsd).
  191. * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  192. */
  193. switch (language) {
  194. case 'az':
  195. case 'bo':
  196. case 'dz':
  197. case 'id':
  198. case 'ja':
  199. case 'jv':
  200. case 'ka':
  201. case 'km':
  202. case 'kn':
  203. case 'ko':
  204. case 'ms':
  205. case 'th':
  206. case 'tr':
  207. case 'vi':
  208. case 'zh':
  209. return 0
  210. case 'af':
  211. case 'bn':
  212. case 'bg':
  213. case 'ca':
  214. case 'da':
  215. case 'de':
  216. case 'el':
  217. case 'en':
  218. case 'eo':
  219. case 'es':
  220. case 'et':
  221. case 'eu':
  222. case 'fa':
  223. case 'fi':
  224. case 'fo':
  225. case 'fur':
  226. case 'fy':
  227. case 'gl':
  228. case 'gu':
  229. case 'ha':
  230. case 'he':
  231. case 'hu':
  232. case 'is':
  233. case 'it':
  234. case 'ku':
  235. case 'lb':
  236. case 'ml':
  237. case 'mn':
  238. case 'mr':
  239. case 'nah':
  240. case 'nb':
  241. case 'ne':
  242. case 'nl':
  243. case 'nn':
  244. case 'no':
  245. case 'oc':
  246. case 'om':
  247. case 'or':
  248. case 'pa':
  249. case 'pap':
  250. case 'ps':
  251. case 'pt':
  252. case 'so':
  253. case 'sq':
  254. case 'sv':
  255. case 'sw':
  256. case 'ta':
  257. case 'te':
  258. case 'tk':
  259. case 'ur':
  260. case 'zu':
  261. return (number === 1) ? 0 : 1
  262. case 'am':
  263. case 'bh':
  264. case 'fil':
  265. case 'fr':
  266. case 'gun':
  267. case 'hi':
  268. case 'hy':
  269. case 'ln':
  270. case 'mg':
  271. case 'nso':
  272. case 'xbr':
  273. case 'ti':
  274. case 'wa':
  275. return ((number === 0) || (number === 1)) ? 0 : 1
  276. case 'be':
  277. case 'bs':
  278. case 'hr':
  279. case 'ru':
  280. case 'sh':
  281. case 'sr':
  282. case 'uk':
  283. return ((number % 10 === 1) && (number % 100 !== 11)) ? 0 : (((number % 10 >= 2) && (number % 10 <= 4) && ((number % 100 < 10) || (number % 100 >= 20))) ? 1 : 2)
  284. case 'cs':
  285. case 'sk':
  286. return (number === 1) ? 0 : (((number >= 2) && (number <= 4)) ? 1 : 2)
  287. case 'ga':
  288. return (number === 1) ? 0 : ((number === 2) ? 1 : 2)
  289. case 'lt':
  290. return ((number % 10 === 1) && (number % 100 !== 11)) ? 0 : (((number % 10 >= 2) && ((number % 100 < 10) || (number % 100 >= 20))) ? 1 : 2)
  291. case 'sl':
  292. return (number % 100 === 1) ? 0 : ((number % 100 === 2) ? 1 : (((number % 100 === 3) || (number % 100 === 4)) ? 2 : 3))
  293. case 'mk':
  294. return (number % 10 === 1) ? 0 : 1
  295. case 'mt':
  296. return (number === 1) ? 0 : (((number === 0) || ((number % 100 > 1) && (number % 100 < 11))) ? 1 : (((number % 100 > 10) && (number % 100 < 20)) ? 2 : 3))
  297. case 'lv':
  298. return (number === 0) ? 0 : (((number % 10 === 1) && (number % 100 !== 11)) ? 1 : 2)
  299. case 'pl':
  300. return (number === 1) ? 0 : (((number % 10 >= 2) && (number % 10 <= 4) && ((number % 100 < 12) || (number % 100 > 14))) ? 1 : 2)
  301. case 'cy':
  302. return (number === 1) ? 0 : ((number === 2) ? 1 : (((number === 8) || (number === 11)) ? 2 : 3))
  303. case 'ro':
  304. return (number === 1) ? 0 : (((number === 0) || ((number % 100 > 0) && (number % 100 < 20))) ? 1 : 2)
  305. case 'ar':
  306. return (number === 0) ? 0 : ((number === 1) ? 1 : ((number === 2) ? 2 : (((number % 100 >= 3) && (number % 100 <= 10)) ? 3 : (((number % 100 >= 11) && (number % 100 <= 99)) ? 4 : 5))))
  307. default:
  308. return 0
  309. }
  310. },
  311. }
  312. export default L10n
  313. /**
  314. * Returns the user's locale
  315. *
  316. * @return {string} locale string
  317. */
  318. export const getLocale = () => $('html').data('locale') ?? 'en'
  319. /**
  320. * Returns the user's language
  321. *
  322. * @return {string} language string
  323. */
  324. export const getLanguage = () => $('html').prop('lang')
  325. Handlebars.registerHelper('t', function(app, text) {
  326. return L10n.translate(app, text)
  327. })