Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support i18n #782

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Setup i18n
  • Loading branch information
skipness committed May 24, 2023
commit f7c462b793635efa6d4e043b2fbbdbbd2360b84e
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {LogBox} from 'react-native'
LogBox.ignoreLogs(['Require cycle:']) // suppress require-cycle warnings, it's fine

import 'platform/polyfills'
import './src/locale/i18n'
import {registerRootComponent} from 'expo'

import App from './src/App'
Expand Down
17 changes: 17 additions & 0 deletions src/locale/en/auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"create.acc": {
"step1": {
"enter.provider.address": "Enter the $t(common:address, lowercase) of your $t(glossary:provider, lowercase):",
"enter.provider.address.placeholder": "Hosting $t(glossary:provider, lowercase) $t(common:address, lowercase)",
"help.tip": "You can change hosting providers at any time.",
"keep.you.online": "This is the service that keeps you online.",
"option1.label": "\u00a0($t(common:default, lowercase))",
"step.title": "Your hosting $t(glossary:provider, lowercase)"
}
},
"splash.screen": {
"create.acc": "Create a new $t(common:account, lowercase)",
"see.what.next": "See what's next",
"sign.in": "Sign in"
}
}
6 changes: 6 additions & 0 deletions src/locale/en/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"account": "Account",
"address": "Address",
"default": "Default",
"other": "Other"
}
4 changes: 4 additions & 0 deletions src/locale/en/glossary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"bluesky": "Bluesky",
"provider": "Provider"
}
26 changes: 26 additions & 0 deletions src/locale/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {getLocales} from 'expo-localization'
import type {LanguageDetectorAsyncModule} from 'i18next'
import i18n from 'i18next'
import {initReactI18next} from 'react-i18next'
import resources from './resources'

const languageDetector: LanguageDetectorAsyncModule = {
type: 'languageDetector',
async: true,
detect: callback => {
callback(getLocales().at(0)?.languageTag)
},
}

i18n.use(languageDetector).use(initReactI18next).init({
debug: true,
fallbackLng: 'en',
resources,
})

// Make sure you add your custom format function AFTER the i18n.init() call.
i18n.services.formatter?.add('lowercase', value => {
return value.toLowerCase()
})

export default i18n
9 changes: 9 additions & 0 deletions src/locale/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import en_auth from './en/auth.json'
import en_common from './en/common.json'
import en_glossary from './en/glossary.json'

const resources = {
en: {auth: en_auth, common: en_common, glossary: en_glossary},
} as const

export default resources
8 changes: 8 additions & 0 deletions src/locale/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'i18next'
import resources from './resources'

declare module 'i18next' {
interface CustomTypeOptions {
resources: (typeof resources)[keyof typeof resources]
}
}
1 change: 1 addition & 0 deletions src/platform/polyfills.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'fast-text-encoding'
import 'intl-pluralrules'
import Graphemer from 'graphemer'
export {}

Expand Down