feat(translation): ES6 support

This commit is contained in:
hristoterezov 2016-10-31 17:16:30 -05:00
parent 1f200b63a7
commit e731f6c3f3
5 changed files with 40 additions and 34 deletions

3
app.js
View File

@ -29,6 +29,7 @@ import API from './modules/API/API';
import UIEvents from './service/UI/UIEvents'; import UIEvents from './service/UI/UIEvents';
import getTokenData from "./modules/TokenData/TokenData"; import getTokenData from "./modules/TokenData/TokenData";
import translation from "./modules/translation/translation";
/** /**
* Tries to push history state with the following parameters: * Tries to push history state with the following parameters:
@ -95,6 +96,7 @@ const APP = {
UI, UI,
settings, settings,
conference, conference,
translation,
/** /**
* After the APP has been initialized provides utility methods for dealing * After the APP has been initialized provides utility methods for dealing
* with the conference room URL(address). * with the conference room URL(address).
@ -106,7 +108,6 @@ const APP = {
init () { init () {
this.keyboardshortcut = this.keyboardshortcut =
require("./modules/keyboardshortcut/keyboardshortcut"); require("./modules/keyboardshortcut/keyboardshortcut");
this.translation = require("./modules/translation/translation");
this.configFetch = require("./modules/config/HttpConfigFetch"); this.configFetch = require("./modules/config/HttpConfigFetch");
this.tokenData = getTokenData(); this.tokenData = getTokenData();
} }

View File

@ -1,6 +1,5 @@
{ {
"en": "English", "en": "English",
"bg": "Bulgarian", "bg": "Bulgarian",
"de": "German", "de": "German",
"es": "Spanish", "es": "Spanish",

View File

@ -2,12 +2,13 @@
import i18n from 'i18next'; import i18n from 'i18next';
import XHR from 'i18next-xhr-backend'; import XHR from 'i18next-xhr-backend';
import jqueryI18next from 'jquery-i18next'; import jqueryI18next from 'jquery-i18next';
var languages = require("../../service/translation/languages"); import languagesR from "../../lang/languages.json";
var languagesR = require("json!../../lang/languages.json"); import mainR from "../../lang/main.json";
var mainR = require("json!../../lang/main.json"); import languages from "../../service/translation/languages";
var DEFAULT_LANG = languages.EN;
var defaultOptions = { const DEFAULT_LANG = languages.EN;
const defaultOptions = {
compatibilityAPI: 'v1', compatibilityAPI: 'v1',
compatibilityJSON: 'v1', compatibilityJSON: 'v1',
fallbackLng: DEFAULT_LANG, fallbackLng: DEFAULT_LANG,
@ -41,8 +42,8 @@ function getLangFromQuery() {
return null; return null;
} }
module.exports = { class Translation {
init: function (settingsLang) { init (settingsLang) {
let options = defaultOptions; let options = defaultOptions;
let lang = getLangFromQuery() || settingsLang || config.defaultLanguage; let lang = getLangFromQuery() || settingsLang || config.defaultLanguage;
@ -65,9 +66,8 @@ module.exports = {
.use({ .use({
type: 'postProcessor', type: 'postProcessor',
name: "resolveAppName", name: "resolveAppName",
process: process: (res, key) => {
function (res, key) { return i18n.t(key, {app: options.app});
return i18n.t(key, {app: interfaceConfig.APP_NAME});
} }
}) })
.init(options, initCompleted); .init(options, initCompleted);
@ -76,33 +76,34 @@ module.exports = {
i18n.addResourceBundle( i18n.addResourceBundle(
DEFAULT_LANG, 'languages', languagesR, true, true); DEFAULT_LANG, 'languages', languagesR, true, true);
jqueryI18next.init(i18n, $, {useOptionsAttr: true}); jqueryI18next.init(i18n, $, {useOptionsAttr: true});
}, }
setLanguage: function (lang) {
setLanguage (lang) {
if(!lang) if(!lang)
lang = DEFAULT_LANG; lang = DEFAULT_LANG;
i18n.setLng(lang, defaultOptions, initCompleted); i18n.setLng(lang, defaultOptions, initCompleted);
}, }
getCurrentLanguage: function () {
getCurrentLanguage () {
return i18n.lng(); return i18n.lng();
}, }
translateElement: function (selector, options) {
translateElement (selector, options) {
// i18next expects undefined if options are missing, check if its null // i18next expects undefined if options are missing, check if its null
selector.localize( selector.localize(
options === null ? undefined : options); options === null ? undefined : options);
},
generateTranslationHTML: function (key, options) {
var str = "<span data-i18n=\"" + key + "\"";
if (options) {
str += " data-i18n-options='" + JSON.stringify(options) + "'";
} }
str += ">";
// i18next expects undefined if options ARE missing, check if its null
str += i18n.t(key, options === null ? undefined : options);
str += "</span>";
return str;
}, generateTranslationHTML (key, options) {
addLanguageChangedListener: function(listener) { let optAttr = options
? ` data-i18n-options='${JSON.stringify(options)}'` : "";
let text = i18n.t(key, options === null ? undefined : options);
return `<span data-i18n="${key}"${optAttr}>${text}</span>`;
}
addLanguageChangedListener(listener) {
i18n.on('languageChanged', listener); i18n.on('languageChanged', listener);
} }
}; }
export default new Translation();

View File

@ -1,4 +1,4 @@
module.exports = { export default {
getLanguages : function () { getLanguages : function () {
var languages = []; var languages = [];
for (var lang in this) for (var lang in this)

View File

@ -58,6 +58,11 @@ var config = {
name: '[path][name].[ext]' name: '[path][name].[ext]'
}, },
test: /\.(gif|png|svg)$/ test: /\.(gif|png|svg)$/
},{
//Adds the ability to import json files.
loader: 'json',
exclude: __dirname + '/node_modules/',
test: /\.json$/
}], }],
noParse: [ noParse: [
// Do not parse the files of the Strophe.js library or at least // Do not parse the files of the Strophe.js library or at least