Skip to content

Commit d508fbe

Browse files
committed
Replace react-native-i18n for react-native-localize.
1 parent ad030dd commit d508fbe

File tree

9 files changed

+103
-102
lines changed

9 files changed

+103
-102
lines changed

android/app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ dependencies {
143143
implementation project(':react-native-gesture-handler')
144144
implementation project(':bugsnag-react-native')
145145
implementation project(':realm')
146-
implementation project(':react-native-i18n')
146+
implementation project(':react-native-localize')
147147
implementation project(':react-native-vector-icons')
148148
}
149149

android/app/src/main/java/com/fnp/dziku/MainApplication.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import android.app.Application;
44

5-
import com.AlexanderZaytsev.RNI18n.RNI18nPackage;
65
import com.facebook.react.ReactApplication;
6+
import com.reactcommunity.rnlocalize.RNLocalizePackage;
77
import com.swmansion.gesturehandler.react.RNGestureHandlerPackage;
88
import com.bugsnag.BugsnagReactNative;
99
import com.facebook.react.ReactNativeHost;
@@ -29,11 +29,11 @@ public boolean getUseDeveloperSupport() {
2929
protected List<ReactPackage> getPackages() {
3030
return Arrays.<ReactPackage>asList(
3131
new MainReactPackage(),
32-
new RNGestureHandlerPackage(),
33-
BugsnagReactNative.getPackage(),
34-
new RealmReactPackage(),
35-
new RNI18nPackage(),
36-
new VectorIconsPackage()
32+
new RNGestureHandlerPackage(),
33+
BugsnagReactNative.getPackage(),
34+
new RealmReactPackage(),
35+
new RNLocalizePackage(),
36+
new VectorIconsPackage()
3737
);
3838
}
3939

android/settings.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ include ':bugsnag-react-native'
55
project(':bugsnag-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/bugsnag-react-native/android')
66
include ':realm'
77
project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android')
8-
include ':react-native-i18n'
9-
project(':react-native-i18n').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-i18n/android')
8+
include ':react-native-localize'
9+
project(':react-native-localize').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-localize/android')
1010
include ':react-native-vector-icons'
1111
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')
1212

config/setupTests.js

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ jest.mock('realm');
1313
jest.mock('react-navigation-backhandler', () => ({
1414
AndroidBackHandler: ({ children }) => children,
1515
}));
16+
jest.mock('react-native-localize', () => ({
17+
findBestAvailableLanguage: () => 'en',
18+
}));

ios/Dziku.xcodeproj/project.pbxproj

+51-78
Large diffs are not rendered by default.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
"dependencies": {
1616
"bugsnag-react-native": "^2.10.2",
1717
"dziku-exercises": "^0.2.0",
18+
"i18n-js": "^3.2.1",
1819
"left-pad": "^1.3.0",
1920
"lodash": "^4.17.11",
2021
"moment": "^2.22.1",
2122
"react": "16.6.3",
2223
"react-native": "^0.58.6",
2324
"react-native-calendars": "^1.21.0",
2425
"react-native-gesture-handler": "^1.0.16",
25-
"react-native-i18n": "^2.0.15",
26+
"react-native-localize": "^1.0.4",
2627
"react-native-paper": "^2.11.1",
2728
"react-native-vector-icons": "^4.6.0",
2829
"react-navigation": "^3.3.2",

src/utils/__tests__/date.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getFirstAndLastMonthDay,
1414
getFirstAndLastWeekday,
1515
} from '../date';
16+
import { clearTranslateCache } from '../i18n';
1617

1718
beforeEach(() => {
1819
moment.locale('en');
@@ -76,6 +77,7 @@ describe('getDatePrettyFormat', () => {
7677

7778
moment.locale('es');
7879
I18nJs.locale = 'es';
80+
clearTranslateCache();
7981
expect(
8082
getDatePrettyFormat(
8183
'2018-05-04T00:00:00.000Z',

src/utils/i18n.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
/* @flow */
22

3-
import I18n from 'react-native-i18n';
3+
import * as RNLocalize from 'react-native-localize';
4+
import i18nJS from 'i18n-js';
5+
import memoize from 'lodash/memoize';
46
import { en, es } from './locales';
57

6-
I18n.fallbacks = true;
7-
I18n.translations = {
8+
const translate = memoize(
9+
(key, config) => i18nJS.t(key, config),
10+
(key, config) => (config ? key + JSON.stringify(config) : key)
11+
);
12+
13+
// fallback if no available language fits
14+
const fallback = { languageTag: 'en', isRTL: false };
15+
16+
const { languageTag } =
17+
RNLocalize.findBestAvailableLanguage(['en', 'es']) || fallback;
18+
19+
// clear translation cache
20+
translate.cache.clear();
21+
22+
// set i18n-js config
23+
i18nJS.fallbacks = true;
24+
i18nJS.translations = {
825
en,
926
es,
1027
};
28+
i18nJS.locale = languageTag;
29+
30+
export const clearTranslateCache = () => translate.cache.clear();
31+
32+
const i18n = {
33+
t: translate,
34+
};
1135

12-
export default I18n;
36+
export default i18n;

yarn.lock

+8-10
Original file line numberDiff line numberDiff line change
@@ -3474,10 +3474,10 @@ http-signature@~1.2.0:
34743474
jsprim "^1.2.2"
34753475
sshpk "^1.7.0"
34763476

3477-
i18n-js@3.0.11:
3478-
version "3.0.11"
3479-
resolved "https://registry.yarnpkg.com/i18n-js/-/i18n-js-3.0.11.tgz#f9e96bdb641c5b9d6be12759d7c422089987ef02"
3480-
integrity sha512-v7dG3kYJTQTyox3NqDabPDE/ZotWntyMI9kh4cYi+XlCSnsIR+KBTS2opPyObL8WndnklcLzbNU92FP/mLge3Q==
3477+
i18n-js@^3.2.1:
3478+
version "3.2.1"
3479+
resolved "https://registry.yarnpkg.com/i18n-js/-/i18n-js-3.2.1.tgz#26cea8e327207bebccf1030baa761c5245ceb63b"
3480+
integrity sha512-AN8Ol+7QMcRml338l5W7JeNsJgXZqXHOwF+e2iKu6gqRM87bmbxk3g2CAfapo7+5ASKOhl+0PiH+YP8OjN+rXg==
34813481

34823482
34833483
version "0.4.19"
@@ -6228,12 +6228,10 @@ react-native-gesture-handler@^1.0.16, react-native-gesture-handler@~1.0.14:
62286228
invariant "^2.2.2"
62296229
prop-types "^15.5.10"
62306230

6231-
react-native-i18n@^2.0.15:
6232-
version "2.0.15"
6233-
resolved "https://registry.yarnpkg.com/react-native-i18n/-/react-native-i18n-2.0.15.tgz#09b5a9836116fa7dbd0054c46e2d1014c1ef3c65"
6234-
integrity sha512-V8VwUP0TLda3oJvgt5tdnFaOV7WXPhTjCTLO7sXI3C2SHggSbD4bCUryMzNJhesimJidH21V2Owvj4zAylHoQQ==
6235-
dependencies:
6236-
i18n-js "3.0.11"
6231+
react-native-localize@^1.0.4:
6232+
version "1.0.4"
6233+
resolved "https://registry.yarnpkg.com/react-native-localize/-/react-native-localize-1.0.4.tgz#4fd3c492ded717c1eb15b2c1f744c8c167e12473"
6234+
integrity sha512-KZg0ZZTbZWor3an6SvQz84swBBj0z8kJX1tk4xGN3rE2Ox/uM/dl056qjBrjtyy4j5f78L05aY17TtOh+vW8Ww==
62376235

62386236
react-native-paper@^2.11.1:
62396237
version "2.11.1"

0 commit comments

Comments
 (0)