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

Allow passing locale via page props #171

Closed
bombillazo opened this issue Apr 18, 2023 · 4 comments
Closed

Allow passing locale via page props #171

bombillazo opened this issue Apr 18, 2023 · 4 comments

Comments

@bombillazo
Copy link

bombillazo commented Apr 18, 2023

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch ni18n@1.0.5 for the project I'm working on.

I need to set the locale upon i18next instance init on Next.js during server-side rendering to avoid hydration issues. To do this, I tried using different i18next plugins to get the locale from the cookies, but none worked. Next.js's native locale management didn't help either as they have very aggressive defaults that will take over the lcoale or assign its own config defaults.

What I did to solve this was to get the locale value from the cookies in a middleware, pass the locale to the pageProps and this is then read in the init step using the HOC provided by ni18n.

Here is the diff that solved my problem. I know it should be made more configurable, right now the property for language in pageProps is hardcoded.

diff --git a/node_modules/ni18n/dist/cjs/app-with-i18next/app-with-i18next.js b/node_modules/ni18n/dist/cjs/app-with-i18next/app-with-i18next.js
index 6b5e8ee..e4ea304 100644
--- a/node_modules/ni18n/dist/cjs/app-with-i18next/app-with-i18next.js
+++ b/node_modules/ni18n/dist/cjs/app-with-i18next/app-with-i18next.js
@@ -100,7 +100,7 @@ var appWithI18Next = function (WrappedComponent, options) {
             var ns = __ni18n_server__ || __ni18n_client__
                 ? (0, common_1.uniqueArray)(__spreadArray(__spreadArray([], __read(((__ni18n_server__ === null || __ni18n_server__ === void 0 ? void 0 : __ni18n_server__.ns) || [])), false), __read(((__ni18n_client__ === null || __ni18n_client__ === void 0 ? void 0 : __ni18n_client__.ns) || [])), false))
                 : i18nextOptions.ns;
-            var instance = (0, create_i18n_instance_1.createI18nInstance)(__assign(__assign(__assign(__assign({}, i18nextOptions), { lng: locale }), __ni18n_server__), { ns: ns }), plugins).instance;
+            var instance = (0, create_i18n_instance_1.createI18nInstance)(__assign(__assign(__assign(__assign({}, i18nextOptions), { lng: props.pageProps.language || locale }), __ni18n_server__), { ns: ns }), plugins).instance;
             return instance;
         }, [options, __ni18n_server__, locale]);
         return (react_1.default.createElement(react_i18next_1.I18nextProvider, { i18n: i18nInstance },
diff --git a/node_modules/ni18n/dist/esm/app-with-i18next/app-with-i18next.js b/node_modules/ni18n/dist/esm/app-with-i18next/app-with-i18next.js
index 9ef7d7b..c1feab9 100644
--- a/node_modules/ni18n/dist/esm/app-with-i18next/app-with-i18next.js
+++ b/node_modules/ni18n/dist/esm/app-with-i18next/app-with-i18next.js
@@ -74,7 +74,7 @@ export var appWithI18Next = function (WrappedComponent, options) {
             var ns = __ni18n_server__ || __ni18n_client__
                 ? uniqueArray(__spreadArray(__spreadArray([], __read(((__ni18n_server__ === null || __ni18n_server__ === void 0 ? void 0 : __ni18n_server__.ns) || [])), false), __read(((__ni18n_client__ === null || __ni18n_client__ === void 0 ? void 0 : __ni18n_client__.ns) || [])), false))
                 : i18nextOptions.ns;
-            var instance = createI18nInstance(__assign(__assign(__assign(__assign({}, i18nextOptions), { lng: locale }), __ni18n_server__), { ns: ns }), plugins).instance;
+            var instance = createI18nInstance(__assign(__assign(__assign(__assign({}, i18nextOptions), { lng: props.pageProps.language || locale }), __ni18n_server__), { ns: ns }), plugins).instance;
             return instance;
         }, [options, __ni18n_server__, locale]);
         return (React.createElement(I18nextProvider, { i18n: i18nInstance },

This issue body was partially generated by patch-package.

@vladislavdev58
Copy link

@bombillazo This is already fixed in #168. No release

@vladislavdev58
Copy link

@bombillazo Released version 1.1.0. Use her

@bombillazo
Copy link
Author

Its not working for me, I need to read it from page props when using the appWithI18Next HOC function.

@bombillazo
Copy link
Author

Closing, no longer using ni18n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants