Skip to content

Commit

Permalink
Merge branch 'record-l10n' of https://github.com/Vishalk91-4/sugarizer
Browse files Browse the repository at this point in the history
…into pr/1495
  • Loading branch information
Lionel Laské committed Jan 18, 2024
2 parents 13665bc + 83b261f commit 5d2fe1d
Show file tree
Hide file tree
Showing 23 changed files with 175 additions and 1,676 deletions.
1 change: 0 additions & 1 deletion activities/Record.activity/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
<script src="lib/intro.js"></script>
<script>if (window.module) module = window.module;</script>
<link rel="prefetch" type="application/l10n" href="translate.ini" />
<script data-main="js/loader" src="lib/require.js"></script>
<script type="text/javascript" src="../../cordova.js"></script>
</head>
Expand Down
20 changes: 5 additions & 15 deletions activities/Record.activity/js/activity.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
define(["sugar-web/activity/activity","sugar-web/presence","activity/capture-helper","sugar-web/datastore","webL10n","tutorial","sugar-web/env"], function (activity,presence,captureHelper,datastore,webL10n,tutorial,env) {
define(["sugar-web/activity/activity","sugar-web/presence","activity/capture-helper","sugar-web/datastore","l10n","tutorial","sugar-web/env"], function (activity,presence,captureHelper,datastore,l10n,tutorial,env) {

requirejs(['domReady!'], function (doc) {

window.addEventListener('localized', function() {
window.l10n = webL10n;
if (datastore !== undefined && datastore.localStorage !== undefined) {
var preferences = datastore.localStorage.getValue('sugar_settings');
if (preferences === null || preferences.name === undefined) {
return;
}
if (preferences.language !== undefined) {
if (webL10n.language.code !== preferences.language)
webL10n.language.code = preferences.language;
}
}
if (webL10n.get("by") !== undefined && webL10n.get("by").length > 0) {
captureHelper.by = webL10n.get("by");
window.l10n = l10n;
if (l10n.get("by") !== undefined && l10n.get("by").length > 0) {
captureHelper.by = l10n.get("by");
}
}, false);

Expand All @@ -27,7 +17,7 @@ define(["sugar-web/activity/activity","sugar-web/presence","activity/capture-hel
// Set current language to Sugarizer
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;
l10n.init(language);
});

if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
Expand Down
3 changes: 3 additions & 0 deletions activities/Record.activity/lib/axios.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions activities/Record.activity/lib/l10n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
let l10ndefine = function (i18next, axios) {
const l10n = {language: {direction: "ltr"}};

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) => {
// In web, load language file from server
if (typeof module === 'undefined') {
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
});
} else {
// In node, load language file from disk
const fs = require('fs');
fs.readFile("./locales/" + lang + ".json", (err, data) => {
if (err) {
console.log("Failed to load " + lang + " language: " + err);
resolve(null); // Resolve with null to indicate failure
} else {
resolve(JSON.parse(data));
}
});
}
});
};

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);
i18next.changeLanguage(lang);
triggerLocalizedEvent();
} else {
l10n.init("en");
}
});
} else {
i18next.changeLanguage(lang);
triggerLocalizedEvent();
}
};


function triggerLocalizedEvent() {
if (typeof module === 'undefined') {
const event = new Event("localized");
window.dispatchEvent(event);
}
};

return l10n;
}

if (typeof module !== 'undefined') {
let i18next = require('i18next');
let axios = require('axios');
module.exports = l10ndefine(i18next, axios);
} else {
define(['i18next.min', 'axios.min'], l10ndefine);
}
8 changes: 2 additions & 6 deletions activities/Record.activity/lib/sugar-web/activity/activity.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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) {
"sugar-web/graphics/activitypalette"], function (shortcut, bus, env, datastore, presence, icon, activitypalette) {

'use strict';

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

l10n.start();

function sendPauseEvent() {
var pauseEvent = document.createEvent("CustomEvent");
pauseEvent.initCustomEvent('activityPause', false, false, {
Expand Down
1 change: 0 additions & 1 deletion activities/Record.activity/lib/sugar-web/test/loader.js
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
2 changes: 1 addition & 1 deletion activities/Record.activity/lib/tutorial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(["webL10n"], function (l10n) {
define(["l10n"], function (l10n) {
var tutorial = {};

tutorial.start = function () {
Expand Down
Loading

0 comments on commit 5d2fe1d

Please sign in to comment.