Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/lib/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import { type Language, languages, resources } from './resources';
* cycle `utils.LOCAL` is still undefined, so `getString(undefined)` throws and
* every route that imports `translate` fails to evaluate (expo-router then
* reports "missing default export" for all routes). Read storage directly here.
*
* This initialLanguage() is the single place that maps a device locale to an
* app language — utils.getDefaultLanguage() just reads the resulting
* i18n.language, so the rendered language and the highlighted pill cannot
* disagree.
*/
function isLanguage(value: string | null | undefined): value is Language {
return !!value && (languages as readonly string[]).includes(value);
Expand All @@ -18,8 +23,19 @@ function isLanguage(value: string | null | undefined): value is Language {
function initialLanguage(): Language {
const stored = storage.getString('language');
if (isLanguage(stored)) return stored;
const code = getLocales()[0]?.languageCode;
return isLanguage(code) ? code : 'en';

const loc = getLocales()[0];
if (!loc) return 'en';
// Chinese needs script/region disambiguation — languageCode is 'zh' for
// EVERY Chinese locale, which used to load Traditional on zh-Hans devices.
// Hans script or CN/SG/MY region → Simplified, otherwise Traditional.
const tag = (loc.languageTag ?? '').toLowerCase();
if (tag.startsWith('zh')) {
return tag.includes('hans') || tag.includes('cn') || tag.includes('sg') || tag.includes('my')
? 'zh-CN'
: 'zh';
}
return isLanguage(loc.languageCode) ? loc.languageCode : 'en';
}

void i18n.use(initReactI18next).init({
Expand Down
20 changes: 7 additions & 13 deletions src/lib/i18n/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { storage } from '@/lib/storage';
import { getLocales } from 'expo-localization';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { useMMKVString } from 'react-native-mmkv';
Expand All @@ -13,19 +12,14 @@ export function isLanguage(value: string | null | undefined): value is Language
return !!value && (languages as readonly string[]).includes(value);
}

/**
* The default language — what applies when the user hasn't picked one.
* Reads the i18n instance instead of re-deriving from the device locale, so
* the selector highlights exactly what is rendered (the locale mapping lives
* only in ./index's initialLanguage).
*/
export function getDefaultLanguage(): Language {
const loc = getLocales()[0];
if (!loc) return 'en';
// Chinese needs script/region disambiguation (languageCode is just 'zh'):
// Hans script or CN/SG/MY region → Simplified, otherwise Traditional.
const tag = (loc.languageTag ?? '').toLowerCase();
if (tag.startsWith('zh')) {
return tag.includes('hans') || tag.includes('cn') || tag.includes('sg') || tag.includes('my')
? 'zh-CN'
: 'zh';
}
const code = loc.languageCode;
return isLanguage(code) ? code : 'en';
return isLanguage(i18n.language) ? i18n.language : 'en';
}

/** Read the selected language synchronously (safe at i18n init time). */
Expand Down