Skip to content

Commit

Permalink
Merge branch 'feature/i18next' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Lionel Laské committed Jul 16, 2023
2 parents da2d827 + 229f993 commit 555322f
Show file tree
Hide file tree
Showing 37 changed files with 4,049 additions and 28,419 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Export CSV instead of TXT in Measure.activity
- Export CSV in Stopwatch activity

### Changed
- Migrate localization to i18next

### Fixed
- Chess multiplayer bug when a third player join #1047
- Undefined tutorial step in curriculum activity #1293
Expand Down
8 changes: 4 additions & 4 deletions activities/QRCode.activity/lib/l10n.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
define(['i18next.min', 'axios.min'], function (i18next, axios) {
const l10n = {};
const l10n = {language: {direction: "ltr"}};
let initialized = false;

l10n.init = async (lang) => {
await i18next.init({
lng: lang,
debug: true,
fallbackLng: "en",
resources: {}
}).then(() => {
l10n.language.direction = i18next.dir();
l10n.switchTo(lang);
});
};

l10n.get = (key) => {
return i18next.t(key);
l10n.get = (key, parameter) => {
return i18next.t(key, parameter);
};

l10n.loadLanguageResource = (lang) => {
Expand Down
2 changes: 1 addition & 1 deletion docs/i18nmigrationguide/guide_vanillajs.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Also remove ```l10n.start();``` activity.setup() function.
Now remove ``` "webL10n": "lib/webL10n" ``` from test/loader.js and ``` "webL10n": "github:sugarlabs/webL10n",``` from sugar-web/package.json.

## Step6: Deleting unnecessary files
Delete files locale.ini (ini file) and lib/webL10n.js.
Delete files locale.ini (ini file), po directory and lib/webL10n.js.

Remove statement mentioned below from index.html file.
```
Expand Down
2 changes: 1 addition & 1 deletion docs/i18nmigrationguide/guide_vuejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Also remove ```l10n.start();``` activity.setup() function.
Now remove ``` "webL10n": "lib/webL10n" ``` from test/loader.js and ``` "webL10n": "github:sugarlabs/webL10n",``` from sugar-web/package.json.

## Step5: Deleting unnecessary files
Delete files locale.ini (ini file) and lib/webL10n.js.
Delete files locale.ini (ini file), po directory and lib/webL10n.js.

Remove statement mentioned below from index.html file.
```
Expand Down
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, viewport-fit=cover"/>

<!-- Sugar Web Framework -->
<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
5 changes: 2 additions & 3 deletions js/app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@


// Desktop handling
define(["webL10n", "sugar-web/graphics/icon", "sugar-web/graphics/xocolor", "sugar-web/graphics/radiobuttonsgroup", "sugar-web/datastore", "sugar-web/presence", "settings", "server", "humane", "util", "tutorial", "stats", "autosync", "history", "activities"], function (_l10n, _iconLib, _xoPalette, _radioButtonsGroup, _datastore, _presence, _preferences, _myserver, _humane, _util, _tutorial, _stats, _autosync, _historic, _activities) {
define(["l10n", "sugar-web/graphics/icon", "sugar-web/graphics/xocolor", "sugar-web/graphics/radiobuttonsgroup", "sugar-web/datastore", "sugar-web/presence", "settings", "server", "humane", "util", "tutorial", "stats", "autosync", "history", "activities"], function (_l10n, _iconLib, _xoPalette, _radioButtonsGroup, _datastore, _presence, _preferences, _myserver, _humane, _util, _tutorial, _stats, _autosync, _historic, _activities) {
// Load required library
l10n = _l10n;
l10n.start();
iconLib = _iconLib;
xoPalette = _xoPalette;
radioButtonsGroup = _radioButtonsGroup;
Expand All @@ -19,7 +18,7 @@ define(["webL10n", "sugar-web/graphics/icon", "sugar-web/graphics/xocolor", "sug
historic = _historic;
activities = _activities;
util = _util;
var toload = 2;
var toload = 1;
var preferenceset = false;

// Main program
Expand Down
1 change: 1 addition & 0 deletions lib/i18next.min.js

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions lib/l10n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
define(['i18next.min', 'axios.min'], function (i18next, axios) {
const l10n = {language: {direction: "ltr"}};
let initialized = false;

l10n.init = async (lang) => {
await i18next.init({
lng: lang,
fallbackLng: "en",
resources: {}
}).then(() => {
l10n.language.direction = i18next.dir();
l10n.switchTo(lang);
});
};

l10n.get = (key, parameter) => {
return i18next.t(key, parameter);
};

l10n.loadLanguageResource = (lang) => {
return new Promise((resolve, reject) => {
axios.get("./locales/" + lang + ".json").then((response) => {
resolve(response.data);
}).catch((error) => {
console.log("Failed to load " + lang + " language: " + error);
resolve(null); // Resolve with null to indicate failure
});
});
};

l10n.switchTo = (lang) => {
if (!i18next.hasResourceBundle(lang, "translation")) {
console.log("Loading " + lang + " language");
l10n.loadLanguageResource(lang).then((locales) => {
if (locales !== null) {
i18next.addResourceBundle(lang, "translation", locales);
} else {
l10n.init("en");
}
i18next.changeLanguage(lang);
initialized = true;
triggerLocalizedEvent();
});
} else {
i18next.changeLanguage(lang);
initialized = true;
triggerLocalizedEvent();
}
};


function triggerLocalizedEvent() {
const event = new Event("localized");
window.dispatchEvent(event);
};

return l10n;
});
5 changes: 3 additions & 2 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ define(["sugar-web/datastore"], function (datastore) {
this.color = 22;
this.colorvalue = null;
this.view = 0;
this.language = "en";
this.language = (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language;
var navigatorLanguage = navigator.language;
if (navigatorLanguage) {
if (navigatorLanguage.indexOf("fr") != -1)
Expand Down Expand Up @@ -48,6 +48,7 @@ define(["sugar-web/datastore"], function (datastore) {
var preferences = datastore.localStorage.getValue('sugar_settings');
if (preferences == null || preferences.name === undefined) {
if (then) {
l10n.init(that.language);
then(null);
}
return;
Expand Down Expand Up @@ -84,7 +85,7 @@ define(["sugar-web/datastore"], function (datastore) {
}

// Set language
l10n.language.code = that.language;
l10n.init(that.language);

if (then) {
then(preferences);
Expand Down
7 changes: 2 additions & 5 deletions lib/sugar-web/activity/activity.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
define(["webL10n",
"sugar-web/activity/shortcut",
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,8 +21,6 @@ define(["webL10n",
activity.setup = function () {
bus.listen();

l10n.start();

function sendPauseEvent() {
var pauseEvent = document.createEvent("CustomEvent");
pauseEvent.initCustomEvent('activityPause', false, false, {
Expand Down
2 changes: 1 addition & 1 deletion lib/sugar-web/test/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ requirejs.config({
"sugar-web": ".",
"mustache": "lib/mustache",
"text": "lib/text",
"webL10n": "lib/webL10n"
"l10n": "lib/l10n"
},

// ask Require.js to load these files (all our tests)
Expand Down
2 changes: 1 addition & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


// Utility functions
define(["webL10n","sugar-web/datastore","FileSaver"], function (l10n, datastore, FileSaver) {
define(["l10n","sugar-web/datastore","FileSaver"], function (l10n, datastore, FileSaver) {
var util = {};

var units = [{name:'Years', factor:356 * 24 * 60 * 60},
Expand Down
Loading

0 comments on commit 555322f

Please sign in to comment.