-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate the source files to improve readability.
- Loading branch information
Showing
3 changed files
with
101 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { NativeModules, Platform } from 'react-native'; | ||
|
||
const LINKING_ERROR = | ||
`The package 'react-native-localization-settings' doesn't seem to be linked. Make sure: \n\n` + | ||
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + | ||
'- You rebuilt the app after installing the package\n' + | ||
'- You are not using Expo Go\n'; | ||
|
||
// @ts-expect-error | ||
const isTurboModuleEnabled = global.__turboModuleProxy != null; | ||
|
||
const LocalizationSettingsModule = isTurboModuleEnabled | ||
? require('./NativeLocalizationSettings').default | ||
: NativeModules.LocalizationSettings; | ||
|
||
const LocalizationSettings = LocalizationSettingsModule | ||
? LocalizationSettingsModule | ||
: new Proxy( | ||
{}, | ||
{ | ||
get() { | ||
throw new Error(LINKING_ERROR); | ||
}, | ||
} | ||
); | ||
|
||
/** | ||
* Get language (sync) | ||
* @returns Language in IETF BCP 47 format (like 'en-US') | ||
* @example | ||
* console.log(getLanguage()) | ||
*/ | ||
export function getLanguage(): string { | ||
return LocalizationSettings.getConstants().language.split('_')[0]; | ||
} | ||
|
||
/** | ||
* Get language (async) | ||
* @param fallback - fallback language | ||
* @returns Promise with Language in IETF BCP 47 format (like 'en-US') | ||
* @example | ||
* getLanguageAsync().then(console.log) | ||
*/ | ||
export function getLanguageAsync(fallback?: string): Promise<string> { | ||
return LocalizationSettings.getLanguage() | ||
.then((res: string) => res.split('_')) | ||
.then((res: string[]) => { | ||
if (res[0]) { | ||
return res[0]; | ||
} | ||
if (fallback) { | ||
return fallback; | ||
} | ||
throw new Error('Invalid language format'); | ||
}); | ||
} | ||
|
||
/** | ||
* Set language | ||
* @param language - locale string | ||
* @example | ||
* Usage: | ||
* setLanguage('en-US') | ||
* | ||
* Preferred format: | ||
* IETF BCP 47 format - "en-US" | ||
* | ||
* Other: | ||
* ISO 639-1 format - "en" | ||
* | ||
*/ | ||
export function setLanguage(language: string): void { | ||
LocalizationSettings.setLanguage(language); | ||
} |
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 |
---|---|---|
@@ -1,98 +1,2 @@ | ||
import { NativeModules, Platform } from 'react-native'; | ||
|
||
const LINKING_ERROR = | ||
`The package 'react-native-localization-settings' doesn't seem to be linked. Make sure: \n\n` + | ||
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + | ||
'- You rebuilt the app after installing the package\n' + | ||
'- You are not using Expo Go\n'; | ||
|
||
// @ts-expect-error | ||
const isTurboModuleEnabled = global.__turboModuleProxy != null; | ||
|
||
const LocalizationSettingsModule = isTurboModuleEnabled | ||
? require('./NativeLocalizationSettings').default | ||
: NativeModules.LocalizationSettings; | ||
|
||
const LocalizationSettings = LocalizationSettingsModule | ||
? LocalizationSettingsModule | ||
: new Proxy( | ||
{}, | ||
{ | ||
get() { | ||
throw new Error(LINKING_ERROR); | ||
}, | ||
} | ||
); | ||
|
||
/** | ||
* Get language (sync) | ||
* @returns Language in IETF BCP 47 format (like 'en-US') | ||
* @example | ||
* console.log(getLanguage()) | ||
*/ | ||
export function getLanguage(): string { | ||
return LocalizationSettings.getConstants().language.split('_')[0]; | ||
} | ||
|
||
/** | ||
* Get language (async) | ||
* @param fallback - fallback language | ||
* @returns Promise with Language in IETF BCP 47 format (like 'en-US') | ||
* @example | ||
* getLanguageAsync().then(console.log) | ||
*/ | ||
export function getLanguageAsync(fallback?: string): Promise<string> { | ||
return LocalizationSettings.getLanguage() | ||
.then((res: string) => res.split('_')) | ||
.then((res: string[]) => { | ||
if (res[0]) { | ||
return res[0]; | ||
} | ||
if (fallback) { | ||
return fallback; | ||
} | ||
throw new Error('Invalid language format'); | ||
}); | ||
} | ||
|
||
/** | ||
* Set language | ||
* @param language - locale string | ||
* @example | ||
* Usage: | ||
* setLanguage('en-US') | ||
* | ||
* Preferred format: | ||
* IETF BCP 47 format - "en-US" | ||
* | ||
* Other: | ||
* ISO 639-1 format - "en" | ||
* | ||
*/ | ||
export function setLanguage(language: string): void { | ||
LocalizationSettings.setLanguage(language); | ||
} | ||
|
||
/** | ||
* i18next language detector | ||
* @example | ||
* Usage: | ||
* i18next | ||
* .use(ReactNativeLanguageDetector) | ||
* .init({ | ||
* ... | ||
* }); | ||
*/ | ||
export const ReactNativeLanguageDetector: I18nLanguageDetectorModule = { | ||
type: 'languageDetector', | ||
init: () => {}, | ||
detect: () => getLanguage(), | ||
cacheUserLanguage: (lng: string) => setLanguage(lng), | ||
}; | ||
|
||
interface I18nLanguageDetectorModule { | ||
type: 'languageDetector'; | ||
init?(): void; | ||
detect(): string | readonly string[] | undefined; | ||
cacheUserLanguage?(lng: string): void; | ||
} | ||
export * from './api'; | ||
export * from './languageDetector'; |
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,25 @@ | ||
import { getLanguage, setLanguage } from './api'; | ||
|
||
/** | ||
* i18next language detector | ||
* @example | ||
* Usage: | ||
* i18next | ||
* .use(ReactNativeLanguageDetector) | ||
* .init({ | ||
* ... | ||
* }); | ||
*/ | ||
export const ReactNativeLanguageDetector: I18nLanguageDetectorModule = { | ||
type: 'languageDetector', | ||
init: () => {}, | ||
detect: () => getLanguage(), | ||
cacheUserLanguage: (lng: string) => setLanguage(lng), | ||
}; | ||
|
||
interface I18nLanguageDetectorModule { | ||
type: 'languageDetector'; | ||
init?(): void; | ||
detect(): string | readonly string[] | undefined; | ||
cacheUserLanguage?(lng: string): void; | ||
} |