tml.app.changeLanguage(locale, function (language) { helpers.updateCurrentLocale(tml.options.key, locale); tml.config.currentLanguage = tml.app.getCurrentLanguage(); if (this.tokenizer && this.tokenizer.updateAllNodes) { this.tokenizer.updateAllNodes(); } else { window.location.reload(); } if (tml.utils.isFunction(tml.options.onLanguageChange)) { tml.options.onLanguageChange(language); } tml.emit('language-change', language); }.bind(this));
translate: function (label, description, tokens, options) { if (!tml.app) { throw new Error("Invalid application."); } var params = tml.utils.normalizeParams(label, description, tokens, options); params.label = params.label.replace(/\s\s+/g, ' '); params.options = tml.utils.extend({}, { current_locale: tml.app.current_locale, current_source: tml.app.current_source, current_translator: tml.app.current_translator, block_options: (tml.block_options || []) }, params.options); return tml.app.getCurrentLanguage().translate(params); },
translateElement: function (container) { container = (typeof container === "string") ? document.getElementById(container) : container; tml.config.currentLanguage = tml.app.getCurrentLanguage(); this.tokenizer = new DomTokenizer(container, {}, { debug: false, current_source: tml.app.current_source || 'index', current_translator: tml.app.current_translator }); if (/ded|te/.test(document.readyState)) { this.tokenizer.translateDOM(document.body); this.translateNow(); } else if (mutationObserver) { if (document.body) { this.tokenizer.translateDOM(document.body); } this.translateNow(); } },
getCurrentLanguage: function () { return tml.app.getCurrentLanguage(); },
initApplication: function (options, callback) { var t0 = new Date(); var cookie = helpers.getCookie(options.key); var cache_version = null; if (options.cache && options.cache.version) cache_version = options.cache.version; tml.config.registerApiAdapter('ajax', require('./api_adapters/ajax')); tml.config.api = 'ajax'; tml.config.registerCacheAdapters({ inline: require('./cache_adapters/inline'), browser: require('./cache_adapters/browser') }); options = tml.utils.merge(tml.config, { delayed_flush: true, api: "ajax", current_source: helpers.getCurrentSource(options), current_locale: helpers.getCurrentLocale(options.key, options.current_locale), current_translator: cookie.translator ? new tml.Translator(cookie.translator) : null, accepted_locales: window.navigator.languages, cache: { enabled: true, adapter: "browser", version: cache_version, version_check_interval: options.version_check_interval || 60 // browser will default to every 60 sec } }, options); options.fetch_version = (options.cache.adapter == 'browser' && !cache_version); tml.config.initCache(options.key); // console.log(options); tml.app = new tml.Application({ key: options.key, token: cookie.oauth ? cookie.oauth.token : null, host: options.host, cdn_host: options.cdn_host }); tml.app.init(options, function (err) { if ((options.translateBody || options.translate_body) && mutationObserver) { tml.translateElement(document); } tml.domReady(function () { if ((options.translateBody || options.translate_body) && !mutationObserver) { tml.translateElement(document.body); } var t1 = new Date(); tml.logger.debug("page render took " + (t1 - t0) + " mls"); if ((options.translateTitle || options.translate_title) && document.title !== "") { document.title = tml.translateLabel(document.title); } if (!options.agent) options.agent = {}; if (options.language_selector) { helpers.includeLs(options.language_selector); } helpers.includeAgent(tml.app, { host: options.agent.host, cache: options.agent.cache || 864000000, domains: options.agent.domains || {}, locale: tml.app.current_locale, source: tml.app.current_source, sdk: options.sdk || 'tml-js v' + tml.version, css: tml.app.css, languages: tml.app.languages }, function () { if (typeof(options.onLoad) == "function") { options.onLoad(tml.app); } tml.emit('load'); if (callback) callback(); }); }); }); },