Skip to content

Commit

Permalink
refactor: split files (#28)
Browse files Browse the repository at this point in the history
Separate the source files to improve readability.
  • Loading branch information
jakex7 authored Nov 11, 2024
1 parent a86170a commit 4c9fb22
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 98 deletions.
74 changes: 74 additions & 0 deletions src/api.ts
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);
}
100 changes: 2 additions & 98 deletions src/index.tsx
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';
25 changes: 25 additions & 0 deletions src/languageDetector.ts
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;
}

0 comments on commit 4c9fb22

Please sign in to comment.