-
-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
323 additions
and
2,255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,174 @@ | ||
|
||
|
||
// Localization component | ||
var Localization = { | ||
template: '<div/>', | ||
data: function() { | ||
template: '<div/>', | ||
data: function () { | ||
return { | ||
l10n: null, | ||
code: null, | ||
dictionary: null, | ||
eventReceived: false | ||
} | ||
eventReceived: false, | ||
activityInitialized: false, | ||
units: [ | ||
{ name: 'Years', factor: 356 * 24 * 60 * 60 }, | ||
{ name: 'Months', factor: 30 * 24 * 60 * 60 }, | ||
{ name: 'Weeks', factor: 7 * 24 * 60 * 60 }, | ||
{ name: 'Days', factor: 24 * 60 * 60 }, | ||
{ name: 'Hours', factor: 60 * 60 }, | ||
{ name: 'Minutes', factor: 60 } | ||
], | ||
}; | ||
}, | ||
computed: { | ||
readyToEmit: function () { | ||
return this.dictionary != null && this.activityInitialized; | ||
}, | ||
}, | ||
mounted: function() { | ||
var vm = this; | ||
watch: { | ||
readyToEmit: function (newVal, oldVal) { | ||
if (newVal) { | ||
this.$emit('localized'); | ||
this.eventReceived = true; | ||
} | ||
}, | ||
}, | ||
mounted: function () { | ||
const vm = this; | ||
|
||
if (vm.l10n == null) { | ||
requirejs(["sugar-web/env", "webL10n"], function (env, webL10n) { | ||
env.getEnvironment(function(err, environment) { | ||
vm.l10n = webL10n; | ||
var defaultLanguage = (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language; | ||
var language = environment.user ? environment.user.language : defaultLanguage; | ||
webL10n.language.code = language; | ||
window.addEventListener("localized", function() { | ||
if (!vm.eventReceived) { | ||
vm.code = language; | ||
vm.dictionary = vm.l10n.dictionary; | ||
vm.$emit("localized"); | ||
vm.eventReceived = true; | ||
} | ||
}); | ||
requirejs(['sugar-web/env'], function (env) { | ||
env.getEnvironment((err, environment) => { | ||
// Get default language | ||
const defaultLanguage = | ||
typeof chrome !== 'undefined' && | ||
chrome.app && | ||
chrome.app.runtime | ||
? chrome.i18n.getUILanguage() | ||
: navigator.language; | ||
const language = environment.user | ||
? environment.user.language | ||
: defaultLanguage; | ||
|
||
if (vm.l10n == null) { | ||
vm.loadLanguageFile(language); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
// Activity initialization check | ||
vm.$root.$children.find(function (child) { | ||
if (child.$options.name === 'SugarActivity') { | ||
child.$on('initialized', function () { | ||
vm.activityInitialized = true; | ||
}); | ||
}}); | ||
}, | ||
|
||
methods: { | ||
get: function(str) { | ||
loadLanguageFile: function (language) { | ||
const vm = this; | ||
requirejs(['lib/i18next.min.js', 'lib/axios.min.js'], function (i18next, axios) { | ||
axios.get(`./locales/${language}.json`).then((response) => { | ||
i18next.init( | ||
{ | ||
lng: language, | ||
fallbackLng: 'en', | ||
debug: false, | ||
resources: { | ||
[language]: { | ||
translation: response.data | ||
} | ||
}, | ||
}, | ||
() => { | ||
vm.l10n = i18next; | ||
vm.code = i18next.language; | ||
vm.dictionary = i18next.getResourceBundle(i18next.language, 'translation'); | ||
vm.subscribeLanguageChange(); | ||
vm.activityInitialized = true; | ||
} | ||
); | ||
}).catch((error) => { | ||
vm.loadLanguageFile('en'); // Load default language | ||
console.log(error); | ||
}); | ||
}); | ||
}, | ||
|
||
subscribeLanguageChange: function () { | ||
const vm = this; | ||
requirejs(['lib/i18next.min.js'], function (i18next) { | ||
i18next.on('languageChanged', (lng) => { | ||
vm.code = lng; | ||
vm.dictionary = i18next.getResourceBundle(lng, 'translation'); // Update dictionary with new language | ||
vm.$emit('localized'); | ||
vm.eventReceived = true; | ||
}); | ||
}); | ||
}, | ||
|
||
// Get a string with parameter | ||
get: function (str, params) { | ||
let out = ''; | ||
|
||
if (!this.dictionary) { | ||
return str; | ||
out = str; | ||
} else { | ||
out = this.dictionary[str] || str; | ||
} | ||
var item = this.dictionary[str]; | ||
if (!item || !item.textContent) { | ||
return str; | ||
|
||
// Check params | ||
if (params) { | ||
let paramsInString = out.match(/{{\s*[\w\.]+\s*}}/g); | ||
for (let i in paramsInString) { | ||
let param = paramsInString[i].match(/[\w\.]+/)[0]; | ||
if (params[param]) { | ||
out = out.replace(paramsInString[i], params[param]); | ||
} | ||
} | ||
} | ||
return item.textContent; | ||
} | ||
} | ||
} | ||
return out; | ||
}, | ||
|
||
// Get values for a set of strings on the form of {stringKey1: '', stringKey2: '', ...} | ||
localize: function (strings) { | ||
const vm = this; | ||
Object.keys(strings).forEach((key, index) => { | ||
strings[key] = vm.get(key.substr(6)); | ||
}); | ||
}, | ||
|
||
// Convert a UNIX timestamp to Sugarizer time elapsed string | ||
localizeTimestamp: function (timestamp) { | ||
const maxlevel = 2; | ||
const levels = 0; | ||
let time_period = ''; | ||
let elapsed_seconds = (Date.now() - timestamp) / 1000; | ||
for (let i = 0; i < this.units.length; i++) { | ||
let factor = this.units[i].factor; | ||
|
||
let elapsed_units = Math.floor(elapsed_seconds / factor); | ||
if (elapsed_units > 0) { | ||
if (levels > 0) | ||
time_period += ','; | ||
|
||
time_period += ' ' + elapsed_units + " " + (elapsed_units == 1 ? this.get(this.units[i].name + "_one") : this.get(this.units[i].name + "_other")); | ||
|
||
elapsed_seconds -= elapsed_units * factor; | ||
} | ||
|
||
if (time_period != '') | ||
levels += 1; | ||
|
||
if (levels == maxlevel) | ||
break; | ||
} | ||
|
||
if (levels == 0) { | ||
return this.get('SecondsAgo'); | ||
} | ||
|
||
return this.get('Ago', { time: time_period }); | ||
}, | ||
}, | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.