Skip to content

Commit

Permalink
Allow users to preset the lang in the URL
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrf committed Mar 26, 2024
1 parent 3d824a8 commit afb385a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
4 changes: 2 additions & 2 deletions frontend/src/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { JSX } from "solid-js";
import { Domain } from "./State";
import { Overlay } from "./map/Overlay";
import {Checkbox, Radio} from "./styles/Forms";
import {availableLangsAndLabels, useI18n} from "./i18n";
import {supportedLangsAndLabels, useI18n} from "./i18n";

/** User settings */
export const Settings = (props: {
Expand All @@ -22,7 +22,7 @@ export const Settings = (props: {
<legend>{ m().settingsLanguage() }</legend>
<select onChange={ event => { setLang(event.currentTarget.value as any) } }>
{
availableLangsAndLabels.map(([langTag, label]) => {
supportedLangsAndLabels.map(([langTag, label]) => {
return (langTag === lang()) ?
<option value={ langTag } selected>{ label }</option> :
<option value={ langTag }>{ label }</option>
Expand Down
32 changes: 24 additions & 8 deletions frontend/src/i18n.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ export const usingMessages = (f: (messages: Messages) => string): Accessor<strin
return f(m())
});

const availableLangs: Array<AvailableLanguageTag> = availableLanguageTags as unknown as Array<AvailableLanguageTag>;
// All the languages supported by the UI
// `availableLanguageTags` is automatically generated by the internationalization system
const supportedLangs: Array<AvailableLanguageTag> = availableLanguageTags as unknown as Array<AvailableLanguageTag>;

// WARN: make sure to update this list to keep it consistent with `availableLangs`
export const availableLangsAndLabels: Array<[AvailableLanguageTag, string]> = [
// WARN: make sure to update this list to keep it consistent with `supportedLangs`
export const supportedLangsAndLabels: Array<[AvailableLanguageTag, string]> = [
['de', 'Deutsch'],
['en', 'English'],
['fr', 'Français'],
Expand All @@ -73,21 +75,31 @@ function fetchMessages (lang: AvailableLanguageTag): Promise<Messages> {
type Localized = Component<{ children: JSX.Element }>;

const detectLang = (): AvailableLanguageTag => {
// 1. Look in the local storage
const isSupported = (candidateLang: string): boolean =>
supportedLangs.some(lang => lang === candidateLang);

// 1. Look in the query parameters
const url = new URL(window.location.toString());
const initLangFromQuery = url.searchParams.get('lang');
if (initLangFromQuery !== null && isSupported(initLangFromQuery)) {
return initLangFromQuery as AvailableLanguageTag
}

// 2. Look in the local storage
const initLangFromLocalStorage = window.localStorage.getItem(langKey);
if (initLangFromLocalStorage !== null && availableLangs.some(lang => lang === initLangFromLocalStorage)) {
if (initLangFromLocalStorage !== null && isSupported(initLangFromLocalStorage)) {
return initLangFromLocalStorage as AvailableLanguageTag
}

// 2. Look in the browser preferences
// 3. Look in the browser preferences
if (window.navigator && window.navigator.language && Intl && Intl.Locale) {
const locale = new Intl.Locale(window.navigator.language);
if (availableLangs.some(lang => lang === locale.language)) {
if (isSupported(locale.language)) {
return locale.language as AvailableLanguageTag
}
}

// 3. Fallback to the default lang
// 4. Fallback to the default lang
return sourceLanguageTag as AvailableLanguageTag
};

Expand All @@ -108,6 +120,10 @@ export const Localized: Localized = lazy(() => {
const lang = getLang();
window.localStorage.setItem(langKey, lang);
setLanguageTag(lang);
// Remove the `lang` parameter from the URL to avoid keeping track of the language in the URL
const url = new URL(window.location.toString());
url.searchParams.delete('lang');
window.history.replaceState(null, '', url);
});
const value: I18n = {
lang: getLang,
Expand Down

0 comments on commit afb385a

Please sign in to comment.