From ed20c42138dfd33c27500c10a960e48dd505c975 Mon Sep 17 00:00:00 2001 From: Aral Roca Gomez Date: Fri, 4 Dec 2020 00:07:45 +0100 Subject: [PATCH 1/2] Change way to use i18n config inside I18nProvider --- __tests__/Trans.test.js | 2 +- package.json | 2 +- src/DynamicNamespaces.tsx | 4 ++-- src/I18nProvider.tsx | 10 ++++++---- src/appWithI18n.tsx | 5 ++--- src/index.tsx | 12 ++---------- 6 files changed, 14 insertions(+), 21 deletions(-) diff --git a/__tests__/Trans.test.js b/__tests__/Trans.test.js index 26321b4e..f6097109 100644 --- a/__tests__/Trans.test.js +++ b/__tests__/Trans.test.js @@ -5,7 +5,7 @@ import Trans from '../src/Trans' const TestEnglish = ({ namespaces, logger, ...props }) => { return ( - + ) diff --git a/package.json b/package.json index 144ec76e..ee10b128 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-translate", - "version": "1.0.0-canary.2", + "version": "1.0.0-canary.3", "description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.", "license": "MIT", "keywords": [ diff --git a/src/DynamicNamespaces.tsx b/src/DynamicNamespaces.tsx index 9b34f779..f99b83b9 100644 --- a/src/DynamicNamespaces.tsx +++ b/src/DynamicNamespaces.tsx @@ -9,10 +9,10 @@ export default function DynamicNamespaces({ fallback, children, }: DynamicNamespacesProps): any { - const { lang, loadLocaleFrom } = useTranslation() + const { lang, config = {} } = useTranslation() const [loaded, setLoaded] = useState(false) const [pageNs, setPageNs] = useState([]) - const loadLocale = dynamic || loadLocaleFrom + const loadLocale = dynamic || config.loadLocaleFrom async function loadNamespaces() { if (typeof loadLocale !== 'function') return diff --git a/src/I18nProvider.tsx b/src/I18nProvider.tsx index 107d75cc..bc2e046f 100644 --- a/src/I18nProvider.tsx +++ b/src/I18nProvider.tsx @@ -101,15 +101,15 @@ export default function I18nProvider({ lang: lng, namespaces = {}, children, - logger = missingKeyLogger, - loadLocaleFrom = () => Promise.resolve({}), + config, }: I18nProviderProps) { - const { lang: parentLang } = useTranslation() + const { lang: parentLang, config: parentConfig = {} } = useTranslation() const { locale, defaultLocale } = useRouter() || {} const lang = lng || parentLang || locale || defaultLocale || '' const ns = useContext(NsContext) const allNamespaces = { ...ns, ...namespaces } as Record const pluralRules = new Intl.PluralRules(lang) + const { logger = missingKeyLogger } = config || parentConfig function t( key: string = '', @@ -152,7 +152,9 @@ export default function I18nProvider({ } return ( - + {children} ) diff --git a/src/appWithI18n.tsx b/src/appWithI18n.tsx index e0de6b9c..009d7272 100644 --- a/src/appWithI18n.tsx +++ b/src/appWithI18n.tsx @@ -19,14 +19,13 @@ export default function appWithI18n( } function AppWithTranslations(props: Props) { - const { logger, loadLocaleFrom, defaultLocale } = config + const { defaultLocale } = config return ( diff --git a/src/index.tsx b/src/index.tsx index 82574d9f..e35e070f 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -16,23 +16,15 @@ export interface I18n { query: TranslationQuery | null | undefined ): string t(i18nKey: string | TemplateStringsArray): string - lang: string - loadLocaleFrom?: ( - language: string, - namespace: string - ) => Promise + config?: I18nConfig } export interface I18nProviderProps { lang?: string namespaces?: Record children?: ReactNode - logger?: I18nLogger - loadLocaleFrom?: ( - language: string, - namespace: string - ) => Promise + config?: I18nConfig } export interface TransProps { From 9917a5bf3bd2ec059a9c79ed87673cd2de1a2e96 Mon Sep 17 00:00:00 2001 From: Aral Roca Gomez Date: Fri, 4 Dec 2020 00:17:23 +0100 Subject: [PATCH 2/2] Add default --- src/DynamicNamespaces.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/DynamicNamespaces.tsx b/src/DynamicNamespaces.tsx index f99b83b9..e9c2ee40 100644 --- a/src/DynamicNamespaces.tsx +++ b/src/DynamicNamespaces.tsx @@ -12,7 +12,8 @@ export default function DynamicNamespaces({ const { lang, config = {} } = useTranslation() const [loaded, setLoaded] = useState(false) const [pageNs, setPageNs] = useState([]) - const loadLocale = dynamic || config.loadLocaleFrom + const loadLocale = + dynamic || config.loadLocaleFrom || (() => Promise.resolve({})) async function loadNamespaces() { if (typeof loadLocale !== 'function') return