Skip to content

Commit

Permalink
feat: add defaultLocale option to globalize config and GlobalizeProvider
Browse files Browse the repository at this point in the history
For situations where specified locale is not available or loaded, defaultLocale provides a backup so
that Globalize doesn't just throw.

closes #59
  • Loading branch information
joshswan committed Sep 22, 2020
1 parent c7585b2 commit 2fe9dc1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
7 changes: 7 additions & 0 deletions docs/components/GlobalizeProvider.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default Root;
## Props

- [`currency`](#currency)
- [`defaultLocale`](#defaultLocale)
- [`locale`](#locale)
- [`localeFallback`](#localeFallback)
- [`onError`](#onerror)
Expand All @@ -37,6 +38,12 @@ export default Root;
| :----: | :------: | :-----: | :---------: |
| string | No | `USD` | Default currency to use when formatting. |

### `defaultLocale`

| Type | Required | Default | Description |
| :----: | :------: | :-----: | :---------: |
| string | No | none | Default locale to use when specified locale not available. |

### `locale`

| Type | Required | Default | Description |
Expand Down
6 changes: 5 additions & 1 deletion src/components/GlobalizeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { GlobalizeContext } from '../context';
export interface Props {
children: React.ReactNode;
currency?: string;
defaultLocale?: string;
locale?: string;
localeFallback?: boolean;
onError?(message: string, exception?: Error): void;
Expand All @@ -21,6 +22,7 @@ export interface Props {
export const GlobalizeProvider: React.FC<Props> = ({
children,
currency: currencyCode = 'USD',
defaultLocale,
locale = 'en',
localeFallback: fallback = false,
...options
Expand All @@ -29,6 +31,7 @@ export const GlobalizeProvider: React.FC<Props> = ({
...options,
locale,
currencyCode,
defaultLocale,
fallback,
}));

Expand All @@ -37,9 +40,10 @@ export const GlobalizeProvider: React.FC<Props> = ({
...options,
locale,
currencyCode,
defaultLocale,
fallback,
}));
}, [currencyCode, locale, fallback]);
}, [currencyCode, defaultLocale, locale, fallback]);

return (
<GlobalizeContext.Provider value={globalize}>
Expand Down
11 changes: 10 additions & 1 deletion src/globalize/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ describe('core', () => {
expect(globalize.locale).toBe('en');
});

test('throws locale not found', () => {
test('uses defaultLocale if specified and locale not found', () => {
expect(() => {
createGlobalize({ locale: 'ga', defaultLocale: 'en' });
}).not.toThrow();

const globalize = createGlobalize({ locale: 'ga', defaultLocale: 'en' });
expect(globalize.locale).toBe('en');
});

test('throws when locale not found and no defaultLocale specified', () => {
const message = '[RNGlobalize] CLDR data for the selected language/locale has not been loaded!';

expect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/globalize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function createGlobalize(config: Config): Globalize {
};

if (!localeIsLoaded(cfg.locale)) {
cfg.locale = (cfg.fallback && findFallbackLocale(cfg.locale)) as string;
cfg.locale = (cfg.fallback && findFallbackLocale(cfg.locale)) || cfg.defaultLocale as string;

if (!cfg.locale) {
throw new Error('[RNGlobalize] CLDR data for the selected language/locale has not been loaded!');
Expand Down
1 change: 1 addition & 0 deletions src/globalize/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export interface Formatters {
export interface GlobalizeConfig {
locale: string;
currencyCode: string;
defaultLocale?: string;
fallback: boolean;
onError(message: string, exception?: Error): void;
}
Expand Down

0 comments on commit 2fe9dc1

Please sign in to comment.