Skip to content

Commit

Permalink
Merge pull request #582 from adobe/i18n-fix
Browse files Browse the repository at this point in the history
fix: localization issues
  • Loading branch information
rofe authored Nov 2, 2023
2 parents c6b5504 + e314d6c commit 83c50b0
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 52 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 24 additions & 16 deletions src/extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
getGitHubSettings,
setConfig,
getConfig,
updateProjectConfigs,
populateUrlCache,
queryUrlCache,
setDisplay,
Expand All @@ -46,10 +45,10 @@ const LANGS = [
'fr',
'it',
'ja',
'ko-kr',
'pt-br',
'zh-cn',
'zh-tw',
'ko',
'pt_BR',
'zh_CN',
'zh_TW',
];

/**
Expand Down Expand Up @@ -321,25 +320,31 @@ function toggle(id) {
}

/**
* Retrieves the sidekick language preferred by the user.
* Retrieves the help language based on the language preferred by the user.
* The default language is <code>en</code>.
* @private
* @return {string} The language
*/
function getLanguage() {
return navigator.languages
.map((prefLang) => LANGS.find((lang) => prefLang.toLowerCase().startsWith(lang)))
.filter((lang) => !!lang)[0] || LANGS[0];
function getHelpLanguage() {
const uLang = chrome.i18n.getUILanguage();
const lang = LANGS.find((l) => uLang.replace('-', '_') === l
|| l.startsWith(uLang.split('_')[0])) || LANGS[0];
return lang.replace('_', '-').toLowerCase();
}

/**
* Updates the help content according to the browser language
* while respecting previous user acknowledgements.
*/
async function updateHelpContent() {
const hlxSidekickHelpContent = await getConfig('sync', 'hlxSidekickHelpContent') || [];
log.debug('existing help content', hlxSidekickHelpContent);
const lang = getLanguage();
// don't fetch new help content for at least 4 hours
const helpContentFetched = await getConfig('local', 'hlxSidekickHelpContentFetched');
if ((helpContentFetched || 0) > Date.now() - 14400000) {
return;
}
const helpContent = await getConfig('sync', 'hlxSidekickHelpContent') || [];
log.debug('existing help content', helpContent);
const lang = getHelpLanguage();
const resp = await fetch(`https://www.hlx.live/tools/sidekick/${lang}/help.json`);
if (resp.ok) {
try {
Expand All @@ -362,9 +367,9 @@ async function updateHelpContent() {
return true;
})
.map((incoming) => {
const index = hlxSidekickHelpContent.findIndex((existing) => existing.id === incoming.id);
const index = helpContent.findIndex((existing) => existing.id === incoming.id);
return {
...(index >= 0 ? hlxSidekickHelpContent[index] : {}),
...(index >= 0 ? helpContent[index] : {}),
...incoming,
steps: incoming.steps
.split(',')
Expand All @@ -376,6 +381,10 @@ async function updateHelpContent() {
await setConfig('sync', {
hlxSidekickHelpContent: updatedHelpContent,
});
// remember when help content was last fetched
await setConfig('local', {
hlxSidekickHelpContentFetched: Date.now(),
});
} catch (e) {
log.error('failed to update help content', e);
}
Expand Down Expand Up @@ -732,7 +741,6 @@ const internalActions = {
// });

await updateHelpContent();
await updateProjectConfigs();
await setUserAgentHeader();
await updateAdminAuthHeaderRules();
log.info('sidekick extension initialized');
Expand Down
12 changes: 7 additions & 5 deletions src/extension/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@
'fr',
'it',
'ja',
'ko-kr',
'pt-br',
'zh-cn',
'zh-tw',
'ko',
'pt_BR',
'zh_CN',
'zh_TW',
];

/**
Expand Down Expand Up @@ -529,7 +529,9 @@
*/
function getLanguage() {
return navigator.languages
.map((prefLang) => LANGS.find((lang) => prefLang.toLowerCase().startsWith(lang)))
.map((uLang) => LANGS
.find((lang) => ((uLang.replace('-', '_') === lang || lang.startsWith(uLang.split('-')[0]))
? lang : undefined)))
.filter((lang) => !!lang)[0] || LANGS[0];
}

Expand Down
20 changes: 0 additions & 20 deletions src/extension/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,23 +711,3 @@ export function toggleDisplay(cb) {
setDisplay(!display, cb);
});
}

/**
* Updates the legacy project configurations to the new format.
* @deprecated
* @todo remove
*/
export async function updateProjectConfigs() {
const configs = await getConfig('sync', 'hlxSidekickConfigs');
const projects = await getConfig('sync', 'hlxSidekickProjects');
if (configs && !projects) {
// migrate old to new project configs
for (let i = 0; i < configs.length; i += 1) {
// eslint-disable-next-line no-await-in-loop
await setProject(configs[i]);
}
// TODO: remove old project configs
// await removeConfig('sync', 'hlxSidekickConfigs');
log.info('project config updated');
}
}
11 changes: 0 additions & 11 deletions test/extension/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,17 +335,6 @@ describe('Test extension utils', () => {
})).to.be.true;
});

it('updateProjectConfigs', async () => {
sandbox.spy(window.chrome.storage.sync, 'set');
sandbox.spy(window.chrome.storage.sync, 'remove');
await utils.removeConfig('sync', 'hlxSidekickProjects');
await utils.updateProjectConfigs();
// expect(chrome.storage.sync.remove.calledWith('hlxSidekickConfigs')).to.be.true;
expect(chrome.storage.sync.set.calledWith({
hlxSidekickProjects: ['test/legacy-project'],
})).to.be.true;
});

it('setDisplay', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'set');
await utils.setDisplay(true);
Expand Down

0 comments on commit 83c50b0

Please sign in to comment.