Skip to content

Commit

Permalink
feat: expose and document lib API
Browse files Browse the repository at this point in the history
  • Loading branch information
jakex7 committed Apr 8, 2023
1 parent 9273f2c commit df3e660
Show file tree
Hide file tree
Showing 4 changed files with 10,049 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@evilmartians/lefthook": "^1.2.2",
"@commitlint/config-conventional": "^17.0.2",
"@evilmartians/lefthook": "^1.2.2",
"@react-native-community/eslint-config": "^3.0.2",
"@release-it/conventional-changelog": "^5.0.0",
"@types/jest": "^28.1.2",
Expand Down
4 changes: 3 additions & 1 deletion src/NativeLocalizationSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
multiply(a: number, b: number): Promise<number>;
getLanguage(): Promise<string>;
setLanguage(lang: string): void;
language: string;
}

export default TurboModuleRegistry.getEnforcing<Spec>('LocalizationSettings');
53 changes: 51 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,55 @@ const LocalizationSettings = LocalizationSettingsModule
}
);

export function multiply(a: number, b: number): Promise<number> {
return LocalizationSettings.multiply(a, b);
/**
* Get language
* @returns Language in IETF BCP 47 format (like 'en-US')
* @example
* console.log(getLanguage())
*/
export function getLanguage(): string {
LocalizationSettings.getLanguage();
return LocalizationSettings.language.split('_')[0];
}

/**
* 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;
}

0 comments on commit df3e660

Please sign in to comment.