Skip to content

Commit

Permalink
Merge branch 'pr/1531' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Lionel Laské committed Feb 17, 2024
2 parents 7263b0d + 71e9d6b commit a1f6341
Show file tree
Hide file tree
Showing 20 changed files with 323 additions and 2,255 deletions.
1 change: 0 additions & 1 deletion activities/EbookReader.activity/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<meta charset="utf-8" />
<title>Ebook Reader 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
189 changes: 158 additions & 31 deletions activities/EbookReader.activity/js/l10n.js
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 });
},
},
};
10 changes: 2 additions & 8 deletions activities/EbookReader.activity/lib/axios.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions activities/EbookReader.activity/lib/i18next.min.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
define(["webL10n",
define([
"sugar-web/activity/shortcut",
"sugar-web/bus",
"sugar-web/env",
"sugar-web/datastore",
"sugar-web/presence",
"sugar-web/graphics/icon",
"sugar-web/graphics/activitypalette"], function (
l10n, shortcut, bus, env, datastore, presence, icon, activitypalette) {
shortcut, bus, env, datastore, presence, icon, activitypalette) {

'use strict';

Expand All @@ -22,7 +22,6 @@ define(["webL10n",
activity.setup = function () {
bus.listen();

l10n.start();

function sendPauseEvent() {
var pauseEvent = document.createEvent("CustomEvent");
Expand Down
1 change: 0 additions & 1 deletion activities/EbookReader.activity/lib/sugar-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"volo": {
"baseUrl": "lib",
"dependencies": {
"webL10n": "github:sugarlabs/webL10n",
"mustache": "github:janl/mustache.js/0.7.2",
"text": "github:requirejs/text"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ requirejs.config({
"sugar-web": ".",
"mustache": "lib/mustache",
"text": "lib/text",
"webL10n": "lib/webL10n"
},

// ask Require.js to load these files (all our tests)
Expand Down
Loading

0 comments on commit a1f6341

Please sign in to comment.