Skip to content

Commit

Permalink
Merge branch 'pr/1437' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Lionel Laské committed Jan 3, 2024
2 parents 62f5550 + 10be436 commit e3277a6
Show file tree
Hide file tree
Showing 19 changed files with 993 additions and 8,354 deletions.
1 change: 0 additions & 1 deletion activities/Tangram.activity/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<title>Tangram Activity</title>
<meta name="viewport"
content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, viewport-fit=cover" />
<link rel="prefetch" type="application/l10n" href="locale.ini" />
<link rel="stylesheet" media="not screen and (device-width: 1200px) and (device-height: 900px)"
href="lib/sugar-web/graphics/css/sugar-96dpi.css">
<link rel="stylesheet" media="screen and (device-width: 1200px) and (device-height: 900px)"
Expand Down
2 changes: 1 addition & 1 deletion activities/Tangram.activity/js/DatasetList.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ var DatasetList = {
computed: {
currentCategoryTitle: function () {
let ct = this.$root.SugarL10n.dictionary ? this.$root.SugarL10n.dictionary["Data" + this.dataSetHandler.currentCategories[0].replace(/ /g, "")] : null;
let categoryTitle = ct ? ct.textContent : this.dataSetHandler.currentCategories[0];
let categoryTitle = ct ? ct : this.dataSetHandler.currentCategories[0];
return categoryTitle;
}
},
Expand Down
2 changes: 1 addition & 1 deletion activities/Tangram.activity/js/SettingEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ var SettingEditor = {

currentCategoryTitle: function() {
let ct = this.$root.SugarL10n.dictionary ? this.$root.SugarL10n.dictionary["Data" + this.dataSetHandler.currentCategories[0].replace(/ /g, "")] : null;
let categoryTitle = ct ? ct.textContent : this.dataSetHandler.currentCategories[0];
let categoryTitle = ct ? ct : this.dataSetHandler.currentCategories[0];
return categoryTitle;
}
},
Expand Down
2 changes: 1 addition & 1 deletion activities/Tangram.activity/js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ var app = new Vue({
for (var i = 0; i < this.DataSetHandler.AllCategories.length; i++) {
let index = this.DataSetHandler.currentCategories.findIndex(ele => ele === this.DataSetHandler.AllCategories[i]);
let ct = this.SugarL10n.dictionary ? this.SugarL10n.dictionary["Data" + this.DataSetHandler.AllCategories[i].replace(/ /g, "")] : null;
let categoryTitle = ct ? ct.textContent : this.DataSetHandler.AllCategories[i];
let categoryTitle = ct ? ct : this.DataSetHandler.AllCategories[i];
categoryButtonsContent += `<div id="category-button-` + (i + 1) + `" tangramcategory="` + this.DataSetHandler.AllCategories[i] + `" class="palette-item` + (index !== -1 ? ` palette-item-selected` : ``) + `">` + categoryTitle + `</div>`;
if (this.DataSetHandler.AllCategories[i] === this.tangramCategories[0]) {
changeCategory = false;
Expand Down
141 changes: 81 additions & 60 deletions activities/Tangram.activity/js/components/SugarL10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Vue.component('sugar-localization', {
l10n: null,
code: null,
dictionary: null,
eventReceived: false,
activityInitialized: false,
units: [
{ name: 'Years', factor: 356 * 24 * 60 * 60 },
Expand All @@ -16,70 +15,92 @@ Vue.component('sugar-localization', {
{ name: 'Hours', factor: 60 * 60 },
{ name: 'Minutes', factor: 60 }
],
}
};
},
computed: {
readyToEmit: function () {
return (this.dictionary != null) && this.activityInitialized;
}
return this.dictionary != null && this.activityInitialized;
},
},
watch: {
readyToEmit: function (newVal, oldVal) {
if (newVal) {
this.$emit("localized");
this.eventReceived = true;
this.$emit('localized');
}
}
},
},
mounted: function () {
var 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;
} else if (webL10n.language.code != language) {
webL10n.language.code = language;
}
});
});
});
//Activity initialization check
var SugarActivity = vm.$root.$children.find(function (child) {
return child.$options.name == 'SugarActivity';
});
SugarActivity.$on('initialized', function () {
vm.activityInitialized = true;
const vm = this;

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;

vm.loadLanguageFile(language);
});
}
});

// Activity initialization check
const SugarActivity = vm.$root.$children.find(function (child) {
return child.$options.name == 'SugarActivity';
});
SugarActivity.$on('initialized', function () {
vm.activityInitialized = true;
});
},

methods: {
// Get a string with parameters
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',
resources: {
[language]: {
translation: response.data
}
},
},
() => {
vm.l10n = i18next;
vm.code = i18next.language;
vm.dictionary = i18next.getResourceBundle(i18next.language, 'translation');
}
);
}).catch((error) => {
vm.loadLanguageFile('en'); // Load default language
console.log(error);
});
});
},

// Get a string with parameter
get: function (str, params) {
var out = '';
let out = '';

if (!this.dictionary) {
out = str;
} else {
var item = this.dictionary[str];
if (!item || !item.textContent) {
out = str;
} else {
out = item.textContent;
}
out = this.dictionary[str] || str;
}

// Check params
if(params) {
var paramsInString = out.match(/{{\s*[\w\.]+\s*}}/g);
for (var i in paramsInString) {
var param = paramsInString[i].match(/[\w\.]+/)[0];
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]);
}
Expand All @@ -90,22 +111,22 @@ Vue.component('sugar-localization', {

// Get values for a set of strings on the form of {stringKey1: '', stringKey2: '', ...}
localize: function (strings) {
var vm = this;
Object.keys(strings).forEach(function (key, index) {
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) {
var maxlevel = 2;
var levels = 0;
var time_period = '';
var elapsed_seconds = ((new Date().getTime()) - timestamp) / 1000;
for (var i = 0; i < this.units.length; i++) {
var factor = this.units[i].factor;

var elapsed_units = Math.floor(elapsed_seconds / factor);
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 += ',';
Expand All @@ -123,10 +144,10 @@ Vue.component('sugar-localization', {
}

if (levels == 0) {
return this.get("SecondsAgo");
return this.get('SecondsAgo');
}

return this.get("Ago", { time: time_period });
}
}
return this.get('Ago', { time: time_period });
},
},
});
Loading

0 comments on commit e3277a6

Please sign in to comment.