Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change config way i18provider #369

Merged
merged 2 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/Trans.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Trans from '../src/Trans'

const TestEnglish = ({ namespaces, logger, ...props }) => {
return (
<I18nProvider lang="en" namespaces={namespaces} logger={logger}>
<I18nProvider lang="en" namespaces={namespaces} config={{ logger }}>
<Trans {...props} />
</I18nProvider>
)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
5 changes: 3 additions & 2 deletions src/DynamicNamespaces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ 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<I18nDictionary[]>([])
const loadLocale = dynamic || loadLocaleFrom
const loadLocale =
dynamic || config.loadLocaleFrom || (() => Promise.resolve({}))

async function loadNamespaces() {
if (typeof loadLocale !== 'function') return
Expand Down
10 changes: 6 additions & 4 deletions src/I18nProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Object>
const pluralRules = new Intl.PluralRules(lang)
const { logger = missingKeyLogger } = config || parentConfig

function t(
key: string = '',
Expand Down Expand Up @@ -152,7 +152,9 @@ export default function I18nProvider({
}

return (
<I18nContext.Provider value={{ lang, t, loadLocaleFrom } as I18n}>
<I18nContext.Provider
value={{ lang, t, config: config || parentConfig } as I18n}
>
<NsContext.Provider value={allNamespaces}>{children}</NsContext.Provider>
</I18nContext.Provider>
)
Expand Down
5 changes: 2 additions & 3 deletions src/appWithI18n.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ export default function appWithI18n(
}

function AppWithTranslations(props: Props) {
const { logger, loadLocaleFrom, defaultLocale } = config
const { defaultLocale } = config

return (
<I18nProvider
lang={props.pageProps?.__lang || props.__lang || defaultLocale}
namespaces={props.pageProps?.__namespaces || props.__namespaces}
logger={logger}
loadLocaleFrom={loadLocaleFrom}
config={config}
>
<AppToTranslate {...props} />
</I18nProvider>
Expand Down
12 changes: 2 additions & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<I18nDictionary>
config?: I18nConfig
}

export interface I18nProviderProps {
lang?: string
namespaces?: Record<string, I18nDictionary>
children?: ReactNode
logger?: I18nLogger
loadLocaleFrom?: (
language: string,
namespace: string
) => Promise<I18nDictionary>
config?: I18nConfig
}

export interface TransProps {
Expand Down