Skip to content

Commit

Permalink
use locale lang file for translated strings
Browse files Browse the repository at this point in the history
  • Loading branch information
tddebart committed Jan 21, 2024
1 parent f83eb4b commit 8a96c93
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions SteamDB-addon/SteamDB/browser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let cdn = "https://cdn.jsdelivr.net/gh/SteamDatabase/BrowserExtension@latest/"

browser = {};
browser.storage = {};
browser.storage.sync = {};
Expand All @@ -8,16 +10,40 @@ browser.storage.sync.get = async function (items) {
}

browser.i18n = {};
const langKey = "steamDB_en";
async function getLang() {
if (sessionStorage.getItem(langKey) === null) {
console.log('[SteamDB addon] getting EN lang');

const response = await fetch(cdn + "_locales/en/messages.json");
sessionStorage.setItem(langKey, JSON.stringify(await response.json()));
}
}
getLang();

browser.i18n.getMessage = function (messageKey, substitutions) {
if (!Array.isArray(substitutions)) {
substitutions = [substitutions];
}

let lang = JSON.parse(sessionStorage.getItem(langKey));
if (lang === null || Object.keys(lang).length === 0) {
console.error('SteamDB lang file not loaded in.');
return messageKey;
}

let messageTemplate = lang[messageKey].message;
substitutions.forEach((substitution, index) => {
messageTemplate = messageTemplate.replace(`$${index + 1}`, substitution);
});

browser.i18n.getMessage = function (message, substitutions) {
//TODO: use translation file instead of returning translation id's
return message;
return messageTemplate;
}

browser.runtime = {};

browser.runtime.getURL = function (res) {
return "https://cdn.jsdelivr.net/gh/SteamDatabase/BrowserExtension@latest/" + res;
return cdn + res;
}

let oldCreateElement = document.createElement.bind(document);
Expand Down

0 comments on commit 8a96c93

Please sign in to comment.