i18n: add language aliasing

This commit is contained in:
Xylight committed 2024-08-01 09:50:12 -07:00
1 parent 1804d9efee
commit 4b42e40817
5 files changed
+71 -26

No files matched your search

+1 -12
View File
@@ -629,19 +629,8 @@
"lang": {
"title": "Language",
"description": "The language used for Photon's UI.",
"note": "Languages other than English and Hebrew are community translated, and may be incomplete.",
"en": "English",
"en-US": "English",
"he": "Hebrew",
"et": "Estonian",
"fr": "French",
"de": "German",
"fi": "Finnish",
"pt": "Portuguese",
"zh": "Chinese",
"zh-Hans": "Chinese (Simplified)",
"zh-Hant": "Chinese (Traditional)",
"auto": "Auto",
"community": "Community",
"credits": "Credits",
"useRtl": {
"title": "Use RTL layout",
+20
View File
@@ -51,6 +51,26 @@ const config: Config = {
fallbackLocale: 'en',
}
export const aliases = new Map([
['zh-CN', 'zh-Hans'],
['zh-TW', 'zh-Hant'],
['en-US', 'en'],
['en-GB', 'en'],
['en-AU', 'en'],
['en-CA', 'en'],
['fr-FR', 'fr'],
['fr-CA', 'fr'],
['fr-BE', 'fr'],
['de-DE', 'de'],
['de-AT', 'de'],
['de-CH', 'de'],
['pt-BR', 'pt'],
['pt-PT', 'pt'],
['fi-FI', 'fi'],
['et-EE', 'et'],
['he-IL', 'he'],
])
export const { t, locale, locales, loading, loadTranslations } = new i18n(
config
)
+3 -3
View File
@@ -1,4 +1,4 @@
import { loadTranslations, locales } from '$lib/translations.js'
import { aliases, loadTranslations, locales } from '$lib/translations.js'
import { get } from 'svelte/store'
export const load = async ({ url, request }) => {
@@ -13,8 +13,8 @@ export const load = async ({ url, request }) => {
}
for (const lang of languages.reverse()) {
const splitLang = lang.split(';')[0]
if (availableLangs.includes(splitLang)) {
preferredLanguage = splitLang
if (availableLangs.includes(splitLang) || aliases.get(splitLang)) {
preferredLanguage = aliases.get(splitLang) || splitLang
}
}
await loadTranslations(preferredLanguage)
+3 -3
View File
@@ -1,7 +1,7 @@
import { browser } from '$app/environment'
import { env } from '$env/dynamic/public'
import { userSettings } from '$lib/settings.js'
import { loadTranslations } from '$lib/translations.js'
import { aliases, loadTranslations } from '$lib/translations.js'
import { get } from 'svelte/store'
export let ssr = (env.PUBLIC_SSR_ENABLED?.toLowerCase() ?? 'false') == 'true'
@@ -11,9 +11,9 @@ export const load = async ({}) => {
const initLocale =
get(userSettings)?.language ?? navigator?.language ?? 'en'
console.log(`Loading locale ${initLocale}`)
console.log(`Loading locale ${aliases.get(initLocale) ?? initLocale}`)
await loadTranslations(initLocale)
await loadTranslations(aliases.get(initLocale) ?? initLocale)
}
return
+44 -8
View File
@@ -29,6 +29,7 @@
ChevronRight,
GlobeAmericas,
Icon,
Language,
Plus,
Trash,
} from 'svelte-hero-icons'
@@ -47,6 +48,25 @@
let pin: string = ''
let importing = false
let importText = ''
let localeMap: Map<
string,
{
name: string
translated: number
flag: string
}
> = new Map([
['en', { name: 'English', translated: 1, flag: '🇺🇸' }],
['et', { name: 'Eesti keel', translated: 0.25, flag: '🇪🇪' }],
['fi', { name: 'Suomi', translated: 0.99, flag: '🇫🇮' }],
['fr', { name: 'Français', translated: 0.76, flag: '🇫🇷' }],
['de', { name: 'Deutsch', translated: 0.81, flag: '🇩🇪' }],
['he', { name: 'עברית', translated: 0.39, flag: '🇮🇱' }],
['pt', { name: 'Português', translated: 0.87, flag: '🇵🇹' }],
['zh-Hans', { name: '简体中文', translated: 0.96, flag: '🇨🇳' }],
['zh-Hant', { name: '繁體中文', translated: 0.27, flag: '🇭🇰' }],
])
</script>
<svelte:head>
@@ -207,23 +227,32 @@
</span>
<p slot="description">
{$t('settings.app.lang.description')}
<Note>
{$t('settings.app.lang.note')}
</Note>
<Link href="/translators" highlight class="text-base font-semibold">
{$t('settings.app.lang.credits')}
</Link>
</p>
<!--@ts-ignore-->
<Select bind:value={$userSettings.language}>
<option value={null}>{$t('settings.app.lang.auto')}</option>
<option value={null}>
<Icon src={Language} size="16" mini />
{$t('settings.app.lang.auto')}
</option>
{#each $locales as locale}
{@const mapped = localeMap.get(locale) ?? {
flag: '',
translated: 1,
name: locale,
}}
<option value={locale}>
{$t(`settings.app.lang.${locale}`, { default: locale })}
<span>{mapped?.flag}</span>
<span>{mapped?.name}</span>
<div
class="text-slate-600 dark:text-zinc-400 text-xs ml-auto"
data-hide-selected
data-label="{mapped.translated * 100}%"
></div>
</option>
{/each}
{#if $userSettings.debugInfo}
<option value="dev">Raw Strings</option>
{/if}
</Select>
</Setting>
{#if $locale == 'he'}
@@ -605,3 +634,10 @@
/>
</Section>
</div>
<style>
[data-hide-selected]::before {
content: attr(data-label);
font-size: small;
}
</style>