diff --git a/docs/components/GlobalizeProvider.md b/docs/components/GlobalizeProvider.md index e5054ca..dc44aa6 100644 --- a/docs/components/GlobalizeProvider.md +++ b/docs/components/GlobalizeProvider.md @@ -27,6 +27,7 @@ export default Root; ## Props - [`currency`](#currency) +- [`defaultLocale`](#defaultLocale) - [`locale`](#locale) - [`localeFallback`](#localeFallback) - [`onError`](#onerror) @@ -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 | diff --git a/src/components/GlobalizeProvider.tsx b/src/components/GlobalizeProvider.tsx index 644a8e8..7afe22e 100644 --- a/src/components/GlobalizeProvider.tsx +++ b/src/components/GlobalizeProvider.tsx @@ -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; @@ -21,6 +22,7 @@ export interface Props { export const GlobalizeProvider: React.FC = ({ children, currency: currencyCode = 'USD', + defaultLocale, locale = 'en', localeFallback: fallback = false, ...options @@ -29,6 +31,7 @@ export const GlobalizeProvider: React.FC = ({ ...options, locale, currencyCode, + defaultLocale, fallback, })); @@ -37,9 +40,10 @@ export const GlobalizeProvider: React.FC = ({ ...options, locale, currencyCode, + defaultLocale, fallback, })); - }, [currencyCode, locale, fallback]); + }, [currencyCode, defaultLocale, locale, fallback]); return ( diff --git a/src/globalize/__tests__/index.test.ts b/src/globalize/__tests__/index.test.ts index a5fd875..2b8500e 100644 --- a/src/globalize/__tests__/index.test.ts +++ b/src/globalize/__tests__/index.test.ts @@ -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(() => { diff --git a/src/globalize/index.ts b/src/globalize/index.ts index baf0092..1143869 100644 --- a/src/globalize/index.ts +++ b/src/globalize/index.ts @@ -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!'); diff --git a/src/globalize/types.ts b/src/globalize/types.ts index 7203b19..48a1a99 100644 --- a/src/globalize/types.ts +++ b/src/globalize/types.ts @@ -106,6 +106,7 @@ export interface Formatters { export interface GlobalizeConfig { locale: string; currencyCode: string; + defaultLocale?: string; fallback: boolean; onError(message: string, exception?: Error): void; }