LocaleUtil.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. /**
  3. * Collection of locale-specific utility functions on client-side When
  4. * constructor is called, locale (and BiDi config) is retrieved from <html> and
  5. * set as members of this module.
  6. */
  7. define([], function () {
  8. var _singletonInstance = null;
  9. var _LocaleUtil = null;
  10. _LocaleUtil = function LocaleUtil() {
  11. // Singleton
  12. if (_LocaleUtil.prototype._singletonInstance) {
  13. return _LocaleUtil.prototype._singletonInstance;
  14. }
  15. this.locale = this.getLocale();
  16. this.isRTL = this.isLocaleRTL(this.locale);
  17. _LocaleUtil.prototype._singletonInstance = this;
  18. };
  19. /**
  20. * Returns the user locale set to <html> lang attribute from server-side.
  21. * This locale is matched from the supported locales and user's locale
  22. * preference
  23. * @return {string} Language code (e.g. en_CA)
  24. */
  25. _LocaleUtil.prototype.getLocale = function () {
  26. if (!this.locale) {
  27. this.locale = document.documentElement.getAttribute('lang');
  28. }
  29. return this.locale;
  30. };
  31. /**
  32. * Checks if the locale passed in as the argument is a RTL (Right-to-Left)
  33. * language
  34. * @param {string} sLocale - Language code
  35. * @return {boolean} True, if the locale is RTL
  36. */
  37. _LocaleUtil.prototype.isLocaleRTL = function (sLocale) {
  38. if (!this.isRTL) {
  39. var aRtlLocales = ['ar', 'he'];
  40. this.isRTL = aRtlLocales.indexOf(sLocale) >= 0;
  41. }
  42. return this.isRTL;
  43. };
  44. var _static = {
  45. getInstance: function getInstance() {
  46. if (!_singletonInstance) {
  47. _singletonInstance = new _LocaleUtil();
  48. }
  49. return _singletonInstance;
  50. }
  51. };
  52. return _static.getInstance();
  53. });
  54. //# sourceMappingURL=LocaleUtil.js.map