Skip to content

Commit

Permalink
feat: implement i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
gtomitsuka committed Apr 15, 2023
1 parent 454eeed commit 1ea047c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@types/react": "17.0.21"
},
"dependencies": {
"i18n-iso-countries": "^7.6.0",
"libphonenumber-js": "^1.10.24"
},
"peerDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions src/CountryPickerModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import NativeToolbar from './NativeToolbar.ios';

interface CountryPickerProps {
manager: InputManager;
doneButtonText: string;
toolbarStyles?: ViewStyle;
pickerStyles?: ViewStyle;
}

const CountryPickerModal = ({
manager,
doneButtonText,
toolbarStyles,
pickerStyles,
}: CountryPickerProps) => {
Expand Down Expand Up @@ -50,6 +52,7 @@ const CountryPickerModal = ({
<NativeToolbar
darkMode={state.darkMode}
onClick={() => dispatch({ type: 'setHidden', payload: true })}
doneButtonText={doneButtonText}
style={toolbarStyles}
/>
{/* TODO: Android support */}
Expand Down
37 changes: 35 additions & 2 deletions src/usePhoneNumberInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Reducer, Dispatch, useReducer } from 'react';
import { Reducer, Dispatch, useReducer, useMemo } from 'react';
import type { Country } from './types';
import { countries } from './countries';
import { Keyboard } from 'react-native';
Expand All @@ -9,13 +9,15 @@ import {
PhoneNumber,
isValidPhoneNumber,
} from 'libphonenumber-js/max';
import { default as i18nCountries } from 'i18n-iso-countries';

interface PhoneNumberInputOptions {
// specify a default country using its ISO-3601 code (e.g., "GB")
defaultCountry?: string;
darkMode?: boolean;
// optional! we provide a default set of countries
customCountries?: Country[];
locale?: string;
}

interface InputState {
Expand Down Expand Up @@ -64,6 +66,34 @@ const usePhoneNumberInput = (
return tel;
};

const localizedCountries = useMemo(() => {
if (
options.locale == null ||
options.locale === '' ||
options.locale === 'en'
)
return [];

const locale = options.locale;
const list = options.customCountries
? [...options.customCountries]
: [...countries];
const isSupported =
i18nCountries
.getSupportedLanguages()
.findIndex((lang) => locale === lang) !== -1;

if (!isSupported) return [];

list.map((country) => {
return { ...country, name: i18nCountries.getName(country.code, locale) };
});

list.sort((a, b) => a.name.localeCompare(b.name));

return list;
}, [options.locale, options.customCountries]);

const [managerState, managerDispatch] = useReducer<
Reducer<InputState, InputAction>
>(
Expand Down Expand Up @@ -130,7 +160,10 @@ const usePhoneNumberInput = (
countryCode: options.defaultCountry || 'US',
pickerHidden: true,
darkMode: options.darkMode || false,
customCountries: options.customCountries,
customCountries:
localizedCountries.length > 0
? localizedCountries
: options.customCountries,
inputText: '',
formattedText: '',
}
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3875,6 +3875,11 @@ detect-newline@^3.0.0:
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
integrity sha512-TLz+x/vEXm/Y7P7wn1EJFNLxYpUD4TgMosxY6fAVJUnJMbupHBOncxyWUG9OpTaH9EBD7uFI5LfEgmMOc54DsA==

diacritics@1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/diacritics/-/diacritics-1.3.0.tgz#3efa87323ebb863e6696cebb0082d48ff3d6f7a1"
integrity sha512-wlwEkqcsaxvPJML+rDh/2iS824jbREk6DUMUKkEaSlxdYHeS43cClJtsWglvw2RfeXGm6ohKDqsXteJ5sP5enA==

diff-sequences@^28.1.1:
version "28.1.1"
resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-28.1.1.tgz#9989dc731266dc2903457a70e996f3a041913ac6"
Expand Down Expand Up @@ -5163,6 +5168,13 @@ human-signals@^4.3.0:
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-4.3.0.tgz#2095c3cd5afae40049403d4b811235b03879db50"
integrity sha512-zyzVyMjpGBX2+6cDVZeFPCdtOtdsxOeseRhB9tkQ6xXmGUNrcnBzdEKPy3VPNYz+4gy1oukVOXcrJCunSyc6QQ==

i18n-iso-countries@^7.6.0:
version "7.6.0"
resolved "https://registry.yarnpkg.com/i18n-iso-countries/-/i18n-iso-countries-7.6.0.tgz#4e2eac7043210a5552e7fd116b74d4f36a90b960"
integrity sha512-HPKjOUKS0BkjiY4ayrsuFbu7Ock++pXLs+FAOYl4WfTL5L0ploEH68fiRAP6Zev5g/jvMFt54KcPGJcb942wbg==
dependencies:
diacritics "1.3.0"

iconv-lite@0.4.24, iconv-lite@^0.4.24:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
Expand Down

0 comments on commit 1ea047c

Please sign in to comment.