-
Notifications
You must be signed in to change notification settings - Fork 17
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
Recipe: Migrating from i18n-js to react-i18next #177
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ad3dea3
Add initial content for recipe
fpena 033f973
Point to i18n file within Ignite
fpena 1507a48
Rewrite based on new PRs
fpena d99afe7
Adding more content
fpena e89b369
Add manage dependencies section and adjust steps
fpena 6f564f5
Add last step to swap locale with language method
fpena de842db
More improvements
fpena a1147ec
Update title correctly
fpena a30e408
PR feedback and improvements
fpena 1d7cd37
Add example and other fixes
fpena 383eea9
Add pickSupportedLocale to step 3
fpena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Adding more content
- Loading branch information
commit d99afe7f21b49e56c0fb9eede0e1089399b63ef8
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 | ||||
---|---|---|---|---|---|---|
|
@@ -17,12 +17,12 @@ In this guide, we will be going through the steps required to migrate your Ignit | |||||
`i18next` will be included in Ignite's version 10. If you're using an earlier version, this guide provides the necessary steps to successfully complete the migration. | ||||||
|
||||||
Finally, the steps outlined in this guide, are based on the changes outlined in the following two PRs: | ||||||
* [Swap out i18n-js for react-18next](https://github.com/infinitered/ignite/pull/2770) | ||||||
* [Swap out i18n-js for react-18next](https://github.com/infinitered/ignite/pull/2770) | ||||||
* [Fix language switching and update date-fns to v4](https://github.com/infinitered/ignite/pull/2778) | ||||||
|
||||||
## Step 1: Set up initialization logic in App.js | ||||||
## Step 1: Set up initialization logic in app.tsx | ||||||
|
||||||
To ensure that `i18next` finishes initializing before your app proceeds, we recommend adding state and logic to the `App.js` entry file: | ||||||
To ensure that `i18next` finishes initializing before your app proceeds, we recommend adding state and logic to the `app.tsx` entry file: | ||||||
|
||||||
frankcalise marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
1. Create a state variable, `isI18nInitialized`, to track initialization status. | ||||||
2. Use the `useEffect` hook to set the state when initialization completes. | ||||||
|
@@ -35,4 +35,62 @@ useEffect(() => { | |||||
}, []); | ||||||
``` | ||||||
|
||||||
This ensures that your app will wait until i18next is fully initialized before continuing, preventing any issues with missing translations. | ||||||
Additionally, consider including the new state variable in the rendering condition for the app. | ||||||
|
||||||
```js | ||||||
// error-line-start | ||||||
if (!rehydrated || !isNavigationStateRestored || (!areFontsLoaded && !fontLoadError)) { | ||||||
// error-line-end | ||||||
// success-line-start | ||||||
if (!rehydrated || !isNavigationStateRestored || !isI18nInitialized || (!areFontsLoaded && !fontLoadError)) { | ||||||
// success-line-end | ||||||
return null | ||||||
} | ||||||
``` | ||||||
|
||||||
This ensures that your app will wait until `i18next` is fully initialized before continuing, preventing any issues with missing translations. | ||||||
|
||||||
## Step 2: Update the i18n initialization method | ||||||
|
||||||
Next, update your i18n initialization to use `react-i18next`, which also includes RTL (right-to-left) language support and handles locale selection. In a Ingnite generated project, this is located in `app/i18n/i18n.ts`. | ||||||
frankcalise marked this conversation as resolved.
Show resolved
Hide resolved
frankcalise marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
```js | ||||||
|
||||||
import * as i18next from "i18next" | ||||||
|
||||||
frankcalise marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
const initI18n = async () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
await i18n.use(initReactI18next).init({ | ||||||
resources, | ||||||
frankcalise marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
lng: pickSupportedLocale()?.languageTag || fallbackLocale, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
fallbackLng: fallbackLocale, | ||||||
interpolation: { escapeValue: false }, | ||||||
}); | ||||||
|
||||||
const locale = pickSupportedLocale(); | ||||||
if (locale?.textDirection === 'rtl') { | ||||||
I18nManager.allowRTL(true); | ||||||
isRTL = true; | ||||||
} else { | ||||||
I18nManager.allowRTL(false); | ||||||
isRTL = false; | ||||||
} | ||||||
|
||||||
return i18n; | ||||||
}; | ||||||
``` | ||||||
|
||||||
This ensures that supported locales are chosen based on the device’s settings, and RTL is correctly applied when necessary. For more on detail on these changes, check the [this PR](https://github.com/infinitered/ignite/pull/2770). | ||||||
|
||||||
## Step 3: Add intl-pluralrules for i18next and JSON v4 | ||||||
|
||||||
To support pluralization and `i18next`'s JSON v4 format, you’ll need to add the `intl-pluralrules` package: | ||||||
|
||||||
```bash | ||||||
yarn add intl-pluralrules | ||||||
``` | ||||||
|
||||||
Make sure to import this package into your i18n configuration file (`app/i18n/i18n.ts`): | ||||||
|
||||||
```js | ||||||
import 'intl-pluralrules'; | ||||||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.