-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(native): improve React Native support and allow setting language
release-npm
- Loading branch information
Showing
10 changed files
with
151 additions
and
33 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
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,16 +1,16 @@ | ||
import { create } from 'logua' | ||
import { Language } from './types' | ||
import { Language, Languages } from './types' | ||
|
||
export const log = create('epic-language', 'magenta') | ||
|
||
// TODO for validation and country specific locales. | ||
// Intl.getCanonicalLocales("EN-US"); // ["en-US"] | ||
|
||
export const readableLanguage = { | ||
export const readableLanguage: Record<Languages, string> = { | ||
[Language.en]: 'English', | ||
[Language.es]: 'Spanish', | ||
[Language.zh]: 'Chinese', | ||
[Language.de]: 'German', | ||
[Language.fr]: 'French', | ||
[Language.it]: 'Italian', | ||
[Language.es]: 'Idioma', | ||
[Language.zh]: '语言', | ||
[Language.de]: 'Deutsch', | ||
[Language.fr]: 'Français', | ||
[Language.it]: 'Italiano', | ||
} |
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
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,13 +1,38 @@ | ||
import { Text } from 'react-native' | ||
import { create as baseCreate } from './index' | ||
import { Sheet, Sheets, Language } from './types' | ||
import { Text, Platform, NativeModules } from 'react-native' | ||
import { create as baseCreate, readableLanguage, Language } from 'epic-language' | ||
import { Sheet, Sheets } from './types' | ||
|
||
export { Language } | ||
export { Language, readableLanguage } | ||
|
||
function getNativeLanguage(defaultLanguage: Language) { | ||
// https://stackoverflow.com/questions/33468746/whats-the-best-way-to-get-device-locale-in-react-native-ios/42655021 | ||
let locale = | ||
Platform.OS === 'android' | ||
? NativeModules.I18nManager.localeIdentifier | ||
: NativeModules.SettingsManager.settings.AppleLocale | ||
|
||
if (!locale) { | ||
;[locale] = NativeModules.SettingsManager.settings.AppleLanguages | ||
if (locale === undefined) { | ||
locale = defaultLanguage | ||
} | ||
} else { | ||
locale = locale.replace('_', '-') | ||
} | ||
|
||
return locale.substring(0, 2) as Language | ||
} | ||
|
||
export function create<T extends Sheet>( | ||
translations: T, | ||
sheets: Sheets<T>, | ||
defaultLanguage: Language = Language.en, | ||
) { | ||
return baseCreate({ translations, defaultLanguage, sheets, Type: Text }) | ||
return baseCreate({ | ||
translations, | ||
defaultLanguage, | ||
sheets, | ||
Type: Text, | ||
getLanguage: getNativeLanguage, | ||
}) | ||
} |
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
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
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
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
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
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