mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-01 20:55:16 +09:00
fix(i18n): default to US English instead of browser language
This commit is contained in:
1 parent
8586d6cedb
commit
3448c7a404
3 files changed
+12
-30
No files matched your search
@@ -3,7 +3,7 @@ import { activateLocale } from '../i18n';
|
||||
import { setFormatLocale } from '../utils/intl';
|
||||
import {
|
||||
DEFAULT_LANGUAGE, LANGUAGES, STORAGE_KEY,
|
||||
directionFor, languageInfo, matchSupported, navigatorLanguages, resolveLocale,
|
||||
directionFor, languageInfo, matchSupported, resolveLocale,
|
||||
} from '../i18n/resolveLocale';
|
||||
|
||||
import { LocaleContext } from './useLocale.js';
|
||||
@@ -18,11 +18,10 @@ import { LocaleContext } from './useLocale.js';
|
||||
* Exactly the arrangement ThemeProvider/ThemeSync already uses.
|
||||
*/
|
||||
export function LocaleProvider({ children }) {
|
||||
// Synchronous first value: whatever the last session stored, else the
|
||||
// browser's own languages. Anything that needs a round trip would repaint.
|
||||
// Synchronous first value: whatever the last session stored, else English.
|
||||
// Anything that needs a round trip would repaint.
|
||||
const [language, setLanguageState] = useState(() => resolveLocale({
|
||||
stored: readStored(),
|
||||
navigatorLanguages: navigatorLanguages(),
|
||||
}));
|
||||
|
||||
// True once the active bundle is in memory. `en` is bundled, so this is
|
||||
@@ -84,8 +83,7 @@ export function LocaleProvider({ children }) {
|
||||
userLanguage,
|
||||
stored: readStored(),
|
||||
panelDefault,
|
||||
navigatorLanguages: navigatorLanguages(),
|
||||
});
|
||||
});
|
||||
setLanguageState(resolved);
|
||||
// Cache for the next pre-auth paint, but do NOT write it back to the
|
||||
// server: nothing was chosen here, it was only resolved.
|
||||
|
||||
@@ -52,7 +52,6 @@ test('the user preference outranks everything', () => {
|
||||
userLanguage: 'es',
|
||||
stored: 'en',
|
||||
panelDefault: 'en',
|
||||
navigatorLanguages: ['en-US'],
|
||||
supported: SUPPORTED,
|
||||
}), 'es');
|
||||
});
|
||||
@@ -63,25 +62,23 @@ test('the stored value outranks the panel default', () => {
|
||||
assert.equal(resolveLocale({
|
||||
stored: 'es',
|
||||
panelDefault: 'en',
|
||||
navigatorLanguages: ['en-US'],
|
||||
supported: SUPPORTED,
|
||||
}), 'es');
|
||||
});
|
||||
|
||||
test('the panel default outranks the browser', () => {
|
||||
// A panel that has an opinion must beat the operator's OS language.
|
||||
test('the panel default outranks the English default', () => {
|
||||
assert.equal(resolveLocale({
|
||||
panelDefault: 'es',
|
||||
navigatorLanguages: ['en-US', 'en'],
|
||||
supported: SUPPORTED,
|
||||
}), 'es');
|
||||
});
|
||||
|
||||
test('the browser decides on a first visit', () => {
|
||||
test('browser language is ignored; a first visit defaults to English', () => {
|
||||
// Even a Spanish navigator must not pick Spanish: resolveLocale takes no navigator input.
|
||||
assert.equal(resolveLocale({
|
||||
navigatorLanguages: ['es-MX', 'en'],
|
||||
navigatorLanguages: ['es-MX', 'es'],
|
||||
supported: SUPPORTED,
|
||||
}), 'es');
|
||||
}), 'en');
|
||||
});
|
||||
|
||||
test('unsupported candidates are skipped, not fatal', () => {
|
||||
@@ -90,15 +87,13 @@ test('unsupported candidates are skipped, not fatal', () => {
|
||||
userLanguage: 'fr',
|
||||
stored: 'de',
|
||||
panelDefault: 'ja',
|
||||
navigatorLanguages: ['pt-BR', 'es-ES'],
|
||||
supported: SUPPORTED,
|
||||
}), 'es');
|
||||
}), DEFAULT_LANGUAGE);
|
||||
});
|
||||
|
||||
test('falls back to English when nothing matches', () => {
|
||||
assert.equal(resolveLocale({
|
||||
userLanguage: 'fr',
|
||||
navigatorLanguages: ['fr-FR'],
|
||||
supported: SUPPORTED,
|
||||
}), DEFAULT_LANGUAGE);
|
||||
assert.equal(resolveLocale({ supported: SUPPORTED }), DEFAULT_LANGUAGE);
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
// user.language (server, per-user)
|
||||
// -> localStorage 'language' (pre-auth cache; stops the login flash)
|
||||
// -> panel default (system_settings, unauthenticated endpoint)
|
||||
// -> navigator.language (first visit, matched to a shipped bundle)
|
||||
// -> 'en'
|
||||
// -> 'en' (US English; browser language is ignored)
|
||||
//
|
||||
// This mirrors ThemeContext's precedence (workspace -> user -> skin ->
|
||||
// default) deliberately: same problem shape, same solution, one thing to
|
||||
@@ -56,10 +55,9 @@ export function resolveLocale({
|
||||
userLanguage = null,
|
||||
stored = null,
|
||||
panelDefault = null,
|
||||
navigatorLanguages = [],
|
||||
supported = SUPPORTED_CODES,
|
||||
} = {}) {
|
||||
const candidates = [userLanguage, stored, panelDefault, ...navigatorLanguages];
|
||||
const candidates = [userLanguage, stored, panelDefault];
|
||||
for (const candidate of candidates) {
|
||||
const match = matchSupported(candidate, supported);
|
||||
if (match) return match;
|
||||
@@ -77,12 +75,3 @@ export function languageInfo(code) {
|
||||
export function directionFor(code) {
|
||||
return languageInfo(code).dir || 'ltr';
|
||||
}
|
||||
|
||||
/** Browser languages, defensively — `navigator` is absent under node. */
|
||||
export function navigatorLanguages() {
|
||||
if (typeof navigator === 'undefined') return [];
|
||||
if (Array.isArray(navigator.languages) && navigator.languages.length) {
|
||||
return [...navigator.languages];
|
||||
}
|
||||
return navigator.language ? [navigator.language] : [];
|
||||
}
|
||||
Reference in new issue
Block a user