Example #1
0
getBrowserLanguage = function() {

  var browserLang = window.navigator.userLanguage
    || window.navigator.language || window.navigator.browserLanguage;

  var key;

  for (key in TAPi18n.getLanguages()) {
    if (browserLang.match(key)) {
      return key;
    }
  }

  return 'en';
};
Example #2
0
import { Template } from 'meteor/templating';
import { TAPi18n } from 'meteor/tap:i18n';

import '/imports/ui/components/navbar-main.js';

import './settings.html';


Template.Settings_page.helpers({
  languages() {
    var currentLang = TAPi18n.getLanguage();
    var langs = TAPi18n.getLanguages();
    var key;
    var lang;
    var langsArray = [];
    for (key in langs) {
      lang = langs[key];
      lang._id = key;
      if (key === currentLang) {
        lang.selected = true;
      }
      langsArray.push(lang);
    }
    return langsArray;
  }
});


Template.Settings_page.onRendered(function(){

  var instance = Template.instance();
Example #3
0
	window.Apps.getWsListener().unregisterListener(AppEvents.APP_STATUS_CHANGE, instance.onStatusChanged);
	window.Apps.getWsListener().unregisterListener(AppEvents.APP_SETTING_UPDATED, instance.onSettingUpdated);
});

Template.appManage.helpers({
	_(key, ...args) {
		const options = (args.pop()).hash;
		if (!_.isEmpty(args)) {
			options.sprintf = args;
		}

		return Template.instance().__(key, options);
	},
	languages() {
		const languages = TAPi18n.getLanguages();

		let result = Object.keys(languages).map((key) => {
			const language = languages[key];
			return _.extend(language, { key });
		});

		result = _.sortBy(result, 'key');
		result.unshift({
			name: 'Default',
			en: 'Default',
			key: '',
		});
		return result;
	},
	appLanguage(key) {
Example #4
0
Meteor.startup(() => {
	TAPi18n.conf.i18n_files_route = Meteor._relativeToSiteRootUrl('/tap-i18n');
	currentLanguage.set(localStorage.getItem('userLanguage'));

	const availableLanguages = TAPi18n.getLanguages();

	const filterLanguage = (language) => {
		// Fix browsers having all-lowercase language settings eg. pt-br, en-us
		const regex = /([a-z]{2})-([a-z]{2})/;
		const matches = regex.exec(language);
		if (matches) {
			return `${ matches[1] }-${ matches[2].toUpperCase() }`;
		}

		return language;
	};

	const getBrowserLanguage = () => filterLanguage(window.navigator.userLanguage || window.navigator.language);

	const loadMomentLocale = (language) => new Promise((resolve, reject) => {
		if (moment.locales().includes(language.toLowerCase())) {
			resolve(language);
			return;
		}

		Meteor.call('loadLocale', language, (error, localeSrc) => {
			if (error) {
				reject(error);
				return;
			}

			Function(localeSrc).call({ moment });
			resolve(language);
		});
	});

	const applyLanguage = (language = 'en') => {
		language = filterLanguage(language);

		if (!availableLanguages[language]) {
			language = language.split('-').shift();
		}

		if (!language) {
			return;
		}

		document.documentElement.classList[isRtl(language) ? 'add' : 'remove']('rtl');
		TAPi18n.setLanguage(language);
		loadMomentLocale(language).then((locale) => moment.locale(locale), (error) => console.error(error));
	};

	const setLanguage = (language) => {
		const lang = filterLanguage(language);
		currentLanguage.set(lang);
		localStorage.setItem('userLanguage', lang);
	};
	window.setLanguage = setLanguage;

	const defaultUserLanguage = () => RocketChat.settings.get('Language') || getBrowserLanguage() || 'en';
	window.defaultUserLanguage = defaultUserLanguage;

	Tracker.autorun(() => {
		const user = RocketChat.models.Users.findOne(Meteor.userId(), { fields: { language: 1 } });

		setLanguage((user && user.language) || defaultUserLanguage());
	});

	Tracker.autorun(() => {
		if (currentLanguage.get()) {
			applyLanguage(currentLanguage.get());
		}
	});
});