-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add frontend localization support (#18)
closes #17
- Loading branch information
Showing
11 changed files
with
348 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"$schema": "https://raw.githubusercontent.com/lit/lit/main/packages/localize-tools/config.schema.json", | ||
"sourceLocale": "en", | ||
"targetLocales": ["ko"], | ||
"tsConfig": "tsconfig.json", | ||
"output": { | ||
"mode": "runtime", | ||
"outputDir": "src/__generated__/locales", | ||
"localeCodesModule": "src/__generated__/locale-codes.ts" | ||
}, | ||
"interchange": { | ||
"format": "xliff", | ||
"xliffDir": "xliff" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { LitElement, html } from "lit"; | ||
import { shouldPolyfill } from "@formatjs/intl-displaynames/should-polyfill"; | ||
|
||
import { allLocales } from "../__generated__/locale-codes"; | ||
import { getLocale, setLocaleFromUrl } from "../utils/localization"; | ||
import { localized } from "@lit/localize"; | ||
|
||
type LocaleCode = typeof allLocales[number]; | ||
type LocaleNames = { | ||
[L in LocaleCode]: string; | ||
}; | ||
|
||
@localized() | ||
export class LocalePicker extends LitElement { | ||
localeNames?: LocaleNames; | ||
|
||
private setLocaleName = (locale: LocaleCode) => { | ||
this.localeNames![locale] = new Intl.DisplayNames([locale], { | ||
type: "language", | ||
}).of(locale); | ||
}; | ||
|
||
async firstUpdated() { | ||
let isFirstPolyfill = true; | ||
|
||
// Polyfill if needed | ||
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames#browser_compatibility | ||
// TODO actually test if polyfill works in older browser | ||
const polyfill = async (locale: LocaleCode) => { | ||
if (!shouldPolyfill(locale)) { | ||
return; | ||
} | ||
|
||
if (isFirstPolyfill) { | ||
await import("@formatjs/intl-getcanonicallocales/polyfill"); | ||
await import("@formatjs/intl-displaynames/polyfill"); | ||
|
||
isFirstPolyfill = false; | ||
} | ||
|
||
try { | ||
await import("@formatjs/intl-displaynames/locale-data/" + locale); | ||
} catch (e) { | ||
console.debug(e); | ||
} | ||
}; | ||
|
||
await Promise.all( | ||
allLocales.map((locale) => polyfill(locale as LocaleCode)) | ||
); | ||
|
||
this.localeNames = {} as LocaleNames; | ||
allLocales.forEach(this.setLocaleName); | ||
|
||
this.requestUpdate(); | ||
} | ||
|
||
render() { | ||
if (!this.localeNames) { | ||
return; | ||
} | ||
|
||
const selectedLocale = getLocale(); | ||
|
||
return html` | ||
<sl-select value=${selectedLocale} @sl-change=${this.localeChanged}> | ||
${allLocales.map( | ||
(locale) => | ||
html`<sl-menu-item | ||
value=${locale} | ||
?selected=${locale === selectedLocale} | ||
> | ||
${this.localeNames![locale]} | ||
</sl-menu-item>` | ||
)} | ||
</sl-select> | ||
`; | ||
} | ||
|
||
async localeChanged(event: Event) { | ||
const newLocale = (event.target as HTMLSelectElement).value as LocaleCode; | ||
|
||
if (newLocale !== getLocale()) { | ||
const url = new URL(window.location.href); | ||
url.searchParams.set("locale", newLocale); | ||
window.history.pushState(null, "", url.toString()); | ||
setLocaleFromUrl(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { configureLocalization } from "@lit/localize"; | ||
|
||
import { sourceLocale, targetLocales } from "../__generated__/locale-codes"; | ||
|
||
export const { getLocale, setLocale } = configureLocalization({ | ||
sourceLocale, | ||
targetLocales, | ||
loadLocale: (locale: string) => | ||
import(`/src/__generated__/locales/${locale}.ts`), | ||
}); | ||
|
||
export const setLocaleFromUrl = async () => { | ||
const url = new URL(window.location.href); | ||
const locale = url.searchParams.get("locale") || sourceLocale; | ||
await setLocale(locale); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> | ||
<file target-language="ko" source-language="en" original="lit-localize-inputs" datatype="plaintext"> | ||
<body> | ||
<trans-unit id="s47d31e4dbe55f7d9"> | ||
<source>Browsertrix Cloud</source> | ||
<target>Browsertrix Cloud</target> | ||
</trans-unit> | ||
<trans-unit id="sd03ac20f93055ed8"> | ||
<source>My Account</source> | ||
<target>내 계정</target> | ||
</trans-unit> | ||
<trans-unit id="sa03807e44737a915"> | ||
<source>Log Out</source> | ||
<target>로그아웃</target> | ||
</trans-unit> | ||
<trans-unit id="sca974356724f8230"> | ||
<source>Log In</source> | ||
<target>로그인</target> | ||
</trans-unit> | ||
</body> | ||
</file> | ||
</xliff> |
Oops, something went wrong.