l10n.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. /**
  11. * L10N namespace with localization functions.
  12. *
  13. * @namespace
  14. */
  15. OC.L10N = {
  16. /**
  17. * String bundles with app name as key.
  18. * @type {Object.<String,String>}
  19. */
  20. _bundles: {},
  21. /**
  22. * Plural functions, key is app name and value is function.
  23. * @type {Object.<String,Function>}
  24. */
  25. _pluralFunctions: {},
  26. /**
  27. * Load an app's translation bundle if not loaded already.
  28. *
  29. * @param {String} appName name of the app
  30. * @param {Function} callback callback to be called when
  31. * the translations are loaded
  32. * @return {Promise} promise
  33. */
  34. load: function(appName, callback) {
  35. // already available ?
  36. if (this._bundles[appName] || OC.getLocale() === 'en') {
  37. var deferred = $.Deferred();
  38. var promise = deferred.promise();
  39. promise.then(callback);
  40. deferred.resolve();
  41. return promise;
  42. }
  43. var self = this;
  44. var url = OC.filePath(appName, 'l10n', OC.getLocale() + '.json');
  45. // load JSON translation bundle per AJAX
  46. return $.get(url)
  47. .then(
  48. function(result) {
  49. if (result.translations) {
  50. self.register(appName, result.translations, result.pluralForm);
  51. }
  52. })
  53. .then(callback);
  54. },
  55. /**
  56. * Register an app's translation bundle.
  57. *
  58. * @param {String} appName name of the app
  59. * @param {Object<String,String>} bundle
  60. * @param {Function|String} [pluralForm] optional plural function or plural string
  61. */
  62. register: function(appName, bundle, pluralForm) {
  63. var self = this;
  64. if (_.isUndefined(this._bundles[appName])) {
  65. this._bundles[appName] = bundle || {};
  66. // generate plural function based on form
  67. this._pluralFunctions[appName] = this._getPlural;
  68. } else {
  69. // Theme overwriting the default language
  70. _.extend(self._bundles[appName], bundle);
  71. }
  72. },
  73. /**
  74. * Translate a string
  75. * @param {string} app the id of the app for which to translate the string
  76. * @param {string} text the string to translate
  77. * @param [vars] map of placeholder key to value
  78. * @param {number} [count] number to replace %n with
  79. * @param {array} [options] options array
  80. * @param {bool} [options.escape=true] enable/disable auto escape of placeholders (by default enabled)
  81. * @return {string}
  82. */
  83. translate: function(app, text, vars, count, options) {
  84. var defaultOptions = {
  85. escape: true
  86. },
  87. allOptions = options || {};
  88. _.defaults(allOptions, defaultOptions);
  89. // TODO: cache this function to avoid inline recreation
  90. // of the same function over and over again in case
  91. // translate() is used in a loop
  92. var _build = function (text, vars, count) {
  93. return text.replace(/%n/g, count).replace(/{([^{}]*)}/g,
  94. function (a, b) {
  95. var r = vars[b];
  96. if(typeof r === 'string' || typeof r === 'number') {
  97. if(allOptions.escape) {
  98. return DOMPurify.sanitize(escapeHTML(r));
  99. } else {
  100. return DOMPurify.sanitize(r);
  101. }
  102. } else {
  103. return DOMPurify.sanitize(a);
  104. }
  105. }
  106. );
  107. };
  108. var translation = text;
  109. var bundle = this._bundles[app] || {};
  110. var value = bundle[text];
  111. if( typeof(value) !== 'undefined' ){
  112. translation = value;
  113. }
  114. if(typeof vars === 'object' || count !== undefined ) {
  115. return DOMPurify.sanitize(_build(translation, vars, count));
  116. } else {
  117. return DOMPurify.sanitize(translation);
  118. }
  119. },
  120. /**
  121. * Translate a plural string
  122. * @param {string} app the id of the app for which to translate the string
  123. * @param {string} textSingular the string to translate for exactly one object
  124. * @param {string} textPlural the string to translate for n objects
  125. * @param {number} count number to determine whether to use singular or plural
  126. * @param [vars] map of placeholder key to value
  127. * @param {array} [options] options array
  128. * @param {bool} [options.escape=true] enable/disable auto escape of placeholders (by default enabled)
  129. * @return {string} Translated string
  130. */
  131. translatePlural: function(app, textSingular, textPlural, count, vars, options) {
  132. var identifier = '_' + textSingular + '_::_' + textPlural + '_';
  133. var bundle = this._bundles[app] || {};
  134. var value = bundle[identifier];
  135. if( typeof(value) !== 'undefined' ){
  136. var translation = value;
  137. if ($.isArray(translation)) {
  138. var plural = this._pluralFunctions[app](count);
  139. return this.translate(app, translation[plural], vars, count, options);
  140. }
  141. }
  142. if(count === 1) {
  143. return this.translate(app, textSingular, vars, count, options);
  144. }
  145. else{
  146. return this.translate(app, textPlural, vars, count, options);
  147. }
  148. },
  149. /**
  150. * The plural function taken from symfony
  151. *
  152. * @param {number} number
  153. * @returns {number}
  154. * @private
  155. */
  156. _getPlural: function(number) {
  157. var language = OC.getLanguage();
  158. if ('pt_BR' === language) {
  159. // temporary set a locale for brazilian
  160. language = 'xbr';
  161. }
  162. if (typeof language === 'undefined' || language === '') {
  163. return (1 == number) ? 0 : 1;
  164. }
  165. if (language.length > 3) {
  166. language = language.substring(0, language.lastIndexOf('_'));
  167. }
  168. /*
  169. * The plural rules are derived from code of the Zend Framework (2010-09-25),
  170. * which is subject to the new BSD license (http://framework.zend.com/license/new-bsd).
  171. * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  172. */
  173. switch (language) {
  174. case 'az':
  175. case 'bo':
  176. case 'dz':
  177. case 'id':
  178. case 'ja':
  179. case 'jv':
  180. case 'ka':
  181. case 'km':
  182. case 'kn':
  183. case 'ko':
  184. case 'ms':
  185. case 'th':
  186. case 'tr':
  187. case 'vi':
  188. case 'zh':
  189. return 0;
  190. case 'af':
  191. case 'bn':
  192. case 'bg':
  193. case 'ca':
  194. case 'da':
  195. case 'de':
  196. case 'el':
  197. case 'en':
  198. case 'eo':
  199. case 'es':
  200. case 'et':
  201. case 'eu':
  202. case 'fa':
  203. case 'fi':
  204. case 'fo':
  205. case 'fur':
  206. case 'fy':
  207. case 'gl':
  208. case 'gu':
  209. case 'ha':
  210. case 'he':
  211. case 'hu':
  212. case 'is':
  213. case 'it':
  214. case 'ku':
  215. case 'lb':
  216. case 'ml':
  217. case 'mn':
  218. case 'mr':
  219. case 'nah':
  220. case 'nb':
  221. case 'ne':
  222. case 'nl':
  223. case 'nn':
  224. case 'no':
  225. case 'oc':
  226. case 'om':
  227. case 'or':
  228. case 'pa':
  229. case 'pap':
  230. case 'ps':
  231. case 'pt':
  232. case 'so':
  233. case 'sq':
  234. case 'sv':
  235. case 'sw':
  236. case 'ta':
  237. case 'te':
  238. case 'tk':
  239. case 'ur':
  240. case 'zu':
  241. return (1 == number) ? 0 : 1;
  242. case 'am':
  243. case 'bh':
  244. case 'fil':
  245. case 'fr':
  246. case 'gun':
  247. case 'hi':
  248. case 'hy':
  249. case 'ln':
  250. case 'mg':
  251. case 'nso':
  252. case 'xbr':
  253. case 'ti':
  254. case 'wa':
  255. return ((0 == number) || (1 == number)) ? 0 : 1;
  256. case 'be':
  257. case 'bs':
  258. case 'hr':
  259. case 'ru':
  260. case 'sh':
  261. case 'sr':
  262. case 'uk':
  263. return ((1 == number % 10) && (11 != number % 100)) ? 0 : (((number % 10 >= 2) && (number % 10 <= 4) && ((number % 100 < 10) || (number % 100 >= 20))) ? 1 : 2);
  264. case 'cs':
  265. case 'sk':
  266. return (1 == number) ? 0 : (((number >= 2) && (number <= 4)) ? 1 : 2);
  267. case 'ga':
  268. return (1 == number) ? 0 : ((2 == number) ? 1 : 2);
  269. case 'lt':
  270. return ((1 == number % 10) && (11 != number % 100)) ? 0 : (((number % 10 >= 2) && ((number % 100 < 10) || (number % 100 >= 20))) ? 1 : 2);
  271. case 'sl':
  272. return (1 == number % 100) ? 0 : ((2 == number % 100) ? 1 : (((3 == number % 100) || (4 == number % 100)) ? 2 : 3));
  273. case 'mk':
  274. return (1 == number % 10) ? 0 : 1;
  275. case 'mt':
  276. return (1 == number) ? 0 : (((0 == number) || ((number % 100 > 1) && (number % 100 < 11))) ? 1 : (((number % 100 > 10) && (number % 100 < 20)) ? 2 : 3));
  277. case 'lv':
  278. return (0 == number) ? 0 : (((1 == number % 10) && (11 != number % 100)) ? 1 : 2);
  279. case 'pl':
  280. return (1 == number) ? 0 : (((number % 10 >= 2) && (number % 10 <= 4) && ((number % 100 < 12) || (number % 100 > 14))) ? 1 : 2);
  281. case 'cy':
  282. return (1 == number) ? 0 : ((2 == number) ? 1 : (((8 == number) || (11 == number)) ? 2 : 3));
  283. case 'ro':
  284. return (1 == number) ? 0 : (((0 == number) || ((number % 100 > 0) && (number % 100 < 20))) ? 1 : 2);
  285. case 'ar':
  286. return (0 == number) ? 0 : ((1 == number) ? 1 : ((2 == number) ? 2 : (((number % 100 >= 3) && (number % 100 <= 10)) ? 3 : (((number % 100 >= 11) && (number % 100 <= 99)) ? 4 : 5))));
  287. default:
  288. return 0;
  289. }
  290. }
  291. };
  292. /**
  293. * translate a string
  294. * @param {string} app the id of the app for which to translate the string
  295. * @param {string} text the string to translate
  296. * @param [vars] map of placeholder key to value
  297. * @param {number} [count] number to replace %n with
  298. * @return {string}
  299. */
  300. window.t = _.bind(OC.L10N.translate, OC.L10N);
  301. /**
  302. * translate a string
  303. * @param {string} app the id of the app for which to translate the string
  304. * @param {string} text_singular the string to translate for exactly one object
  305. * @param {string} text_plural the string to translate for n objects
  306. * @param {number} count number to determine whether to use singular or plural
  307. * @param [vars] map of placeholder key to value
  308. * @return {string} Translated string
  309. */
  310. window.n = _.bind(OC.L10N.translatePlural, OC.L10N);
  311. Handlebars.registerHelper('t', function(app, text) {
  312. return OC.L10N.translate(app, text);
  313. });