Skip to content

Commit

Permalink
Add local names to locale details
Browse files Browse the repository at this point in the history
  • Loading branch information
bearfriend committed Mar 5, 2025
1 parent 24154ed commit 30ca2a4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
window.intl = { ...common, ...dateTime, ...fileSize, ...list, ...number, ...localize };

render(
common.supportedLocalesDetails.map(({ code, name }) => html`<option value="${code}">${name}</option>`),
common.supportedLocalesDetails.map(({ code, localName }) => html`<option value="${code}">${localName}</option>`),
document.querySelector('#languages')
);
</script>
Expand Down Expand Up @@ -57,6 +57,7 @@ <h2>Usage</h2>
const htmlLangInput = document.getElementById('html-lang');

document.getElementById('apply-lang').onclick = () => {
debugger;
htmlElement.lang = htmlLangInput.value;
};
</script>
Expand Down
21 changes: 17 additions & 4 deletions lib/common.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
export const defaultLocale = 'en';
export const supportedBaseLocales = ['ar', 'cy', 'da', 'de', 'en', 'es', 'fr', 'haw', 'hi', 'ja', 'ko', 'mi', 'nl', 'pt', 'sv', 'tr', 'zh'];
export const supportedLangpacks = ['ar', 'cy', 'da', 'de', 'en', 'en-gb', 'es', 'es-es', 'fr', 'fr-fr', 'fr-on', 'haw', 'hi', 'ja', 'ko', 'mi', 'nl', 'pt', 'sv', 'tr', 'zh-cn', 'zh-tw'];

function updateLocalNames() {
const localName = new Intl.DisplayNames(documentLocaleSettings.language || navigator.language, { type: 'language' });

Check failure on line 6 in lib/common.js

View workflow job for this annotation

GitHub Actions / test

'documentLocaleSettings' was used before it was defined
supportedLocalesDetails.forEach(l => {

Check failure on line 7 in lib/common.js

View workflow job for this annotation

GitHub Actions / test

'supportedLocalesDetails' was used before it was defined
l.localName = localName.of(l.overrideCode || l.code);
});
}

export const supportedLocalesDetails = [
{ code: 'ar-sa', dir: 'rtl', name: 'العربية (المملكة العربية السعودية)' },
{ code: 'cy-gb', dir: 'ltr', name: 'Cymraeg (Y Deyrnas Unedig)' },
Expand All @@ -10,10 +18,10 @@ export const supportedLocalesDetails = [
{ code: 'en-gb', dir: 'ltr', name: 'English (United Kingdom)' },
{ code: 'en-us', dir: 'ltr', name: 'English (United States)' },
{ code: 'es-es', dir: 'ltr', name: 'Español (España)' },
{ code: 'es-mx', dir: 'ltr', name: 'Español (Latinoamérica)' },
{ code: 'es-mx', overrideCode: 'es-419', dir: 'ltr', name: 'Español (Latinoamérica)' },
{ code: 'fr-ca', dir: 'ltr', name: 'Français (Canada)' },
{ code: 'fr-fr', dir: 'ltr', name: 'Français (France)' },
{ code: 'fr-on', dir: 'ltr', name: 'Français (Ontario)' },
{ code: 'fr-on', overrideCode: 'fr-CA-Ontario' , dir: 'ltr', name: 'Français (Ontario)' },

Check failure on line 24 in lib/common.js

View workflow job for this annotation

GitHub Actions / test

There should be no space before ','
{ code: 'haw-us', dir: 'ltr', name: 'ʻŌlelo Hawaiʻi (ʻAmelika Hui Pū ʻIa)' },
{ code: 'hi-in', dir: 'ltr', name: 'हिंदी (भारत)' },
{ code: 'ja-jp', dir: 'ltr', name: '日本語 (日本)' },
Expand All @@ -23,8 +31,8 @@ export const supportedLocalesDetails = [
{ code: 'pt-br', dir: 'ltr', name: 'Português (Brasil)' },
{ code: 'sv-se', dir: 'ltr', name: 'Svenska (Sverige)' },
{ code: 'tr-tr', dir: 'ltr', name: 'Türkçe (Türkiye)' },
{ code: 'zh-cn', dir: 'ltr', name: '中文(简体)' },
{ code: 'zh-tw', dir: 'ltr', name: '中文(繁体)' }
{ code: 'zh-cn', overrideCode: 'zh-hans', dir: 'ltr', name: '中文(简体)' },
{ code: 'zh-tw', overrideCode: 'zh-hant', dir: 'ltr', name: '中文(繁体)' }
];
export const supportedLocales = supportedLocalesDetails.map((l) => l.code);

Expand Down Expand Up @@ -225,3 +233,8 @@ export function getDocumentLocaleSettings() {
}
return documentLocaleSettings;
}

(() => {
getDocumentLocaleSettings().addChangeListener(updateLocalNames);
updateLocalNames();
})()

Check failure on line 240 in lib/common.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

0 comments on commit 30ca2a4

Please sign in to comment.