Await loadLanguageResources and add default banner text
This commit is contained in:
+48
-5
@@ -1,8 +1,13 @@
|
|||||||
const i18n = {};
|
const i18n = {};
|
||||||
const languageDefault = 'en';
|
const languageDefault = 'en';
|
||||||
|
const defaultEngText = {
|
||||||
|
title: 'Download Twake Mail application',
|
||||||
|
description: 'Faster and more convenient',
|
||||||
|
open: 'Open'
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* @description Check if the language is valid
|
||||||
* @param {String} language
|
* @param {String} language
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
@@ -13,6 +18,10 @@ function isValidUserLanguage(language) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Get the user language
|
||||||
|
* @returns {String}
|
||||||
|
*/
|
||||||
function getUserLanguage() {
|
function getUserLanguage() {
|
||||||
console.info(`[Twake Mail] Current Language: `, navigator.language);
|
console.info(`[Twake Mail] Current Language: `, navigator.language);
|
||||||
if (isValidUserLanguage(navigator.language)) {
|
if (isValidUserLanguage(navigator.language)) {
|
||||||
@@ -21,21 +30,55 @@ function getUserLanguage() {
|
|||||||
return languageDefault;
|
return languageDefault;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Load the language resources
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
async function loadLanguageResources() {
|
async function loadLanguageResources() {
|
||||||
|
const language = getUserLanguage();
|
||||||
try {
|
try {
|
||||||
const language = getUserLanguage();
|
|
||||||
const response = await fetch(`/i18n/${language}.json`);
|
const response = await fetch(`/i18n/${language}.json`);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
i18n[language] = data;
|
i18n[language] = data;
|
||||||
console.info(`[Twake Mail] Successfully loaded ${language} resources:`, i18n[language]);
|
console.info(`[Twake Mail] Successfully loaded ${language} resources:`, i18n[language]);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`[Twake Mail] Error loading ${language} resources:`, error);
|
console.error(`[Twake Mail] Error loading ${language} resources:`, error);
|
||||||
|
i18n['en'] = defaultEngText;
|
||||||
|
console.info(`[Twake Mail] Using default English resources:`, i18n['en']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Set the text content of smart banner
|
||||||
|
* @typedef SetTextContentParams
|
||||||
|
* @property {String} id
|
||||||
|
* @property {String} text
|
||||||
|
* @param {SetTextContentParams} param
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
function setTextContent({id, text}) {
|
||||||
|
document.getElementById(id).textContent = text;
|
||||||
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', function () {
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
const language = getUserLanguage();
|
const language = getUserLanguage();
|
||||||
document.getElementById('banner-title-id').textContent = i18n[language].title;
|
|
||||||
document.getElementById('banner-description-id').textContent = i18n[language].description;
|
let languageResources = defaultEngText;
|
||||||
document.getElementById('open-button-id').textContent = i18n[language].open;
|
|
||||||
|
if (i18n[language] !== null && i18n[language] !== undefined) {
|
||||||
|
languageResources = i18n[language];
|
||||||
|
}
|
||||||
|
|
||||||
|
setTextContent({
|
||||||
|
id: 'banner-title-id',
|
||||||
|
text: languageResources.title
|
||||||
|
});
|
||||||
|
setTextContent({
|
||||||
|
id: 'banner-description-id',
|
||||||
|
text: languageResources.description
|
||||||
|
});
|
||||||
|
setTextContent({
|
||||||
|
id: 'open-button-id',
|
||||||
|
text: languageResources.open
|
||||||
|
});
|
||||||
});
|
});
|
||||||
+1
-2
@@ -85,8 +85,7 @@
|
|||||||
application. For more information, see:
|
application. For more information, see:
|
||||||
https://developers.google.com/web/fundamentals/primers/service-workers -->
|
https://developers.google.com/web/fundamentals/primers/service-workers -->
|
||||||
<script>
|
<script>
|
||||||
loadLanguageResources()
|
loadLanguageResources().finally(initialWorkerService);
|
||||||
initialWorkerService();
|
|
||||||
</script>
|
</script>
|
||||||
<script src="https://unpkg.com/pica/dist/pica.min.js" ></script>
|
<script src="https://unpkg.com/pica/dist/pica.min.js" ></script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user