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

next-i18next #5

Merged
merged 3 commits into from
Aug 17, 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
5 changes: 0 additions & 5 deletions nodemon.json

This file was deleted.

9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "nodemon server.js",
"dev": "next dev",
"build": "next build",
"start": "NODE_ENV=production node server.js"
"start": "next start"
},
"dependencies": {
"express": "^4.17.1",
"i18next": "^19.4.5",
"next": "^9.5.0",
"nodemon": "^2.0.4",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-i18next": "^11.5.0"
"react-dom": "16.13.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"home": "Home",
"helloWorld": "Hello World"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"home": "Inicio",
"helloWorld": "Hola mundo"
}
46 changes: 0 additions & 46 deletions server.js

This file was deleted.

8 changes: 3 additions & 5 deletions src/components/Header.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import Link from "next/link";
import { useTranslation } from "../i18n";
import styles from "./Header.module.css";

function Header() {
const { t } = useTranslation();
return (
<header className={styles.header}>
<Link href="/">
<a>Home</a>
</Link>
{" | "}
<Link href="/gip">
<a>getInitialProps</a>
<a>{t('home')}</a>
</Link>
</header>
);
Expand Down
55 changes: 37 additions & 18 deletions src/i18n.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
const NextI18Next = require('next-i18next').default
const path = require('path')

export const supportedLocales = ["en", "de", "es"];
const DEFAULT_LOCALE = 'en';
const supportedLangs = ['en', 'es'];
const domainLocaleMap = {
localhost: "en",
"nexttest.com": "en",
"nexttest.es": "es",
"nexttest.de": "de",
"it.nexttest.international": "it",
"ua.nexttest.international": "ua",
};

export default function initI18N(lang) {
console.log("initI18N: lang:", lang);
i18n.use(initReactI18next);
i18n.init({
fallbackLng: "en",
lng: lang || "en",
resources: {
en: {
translation: {
helloWorld: "Hello world",
},
},
},
});
return i18n;
const domainDetector = {
name: 'domain',
lookup(req, res, options) {
let locale = DEFAULT_LOCALE;
if (typeof window !== 'undefined' ) {
locale = domainLocaleMap[window.location.hostname];
} else {
const hostname = req.headers.host?.split(':')[0];
locale = domainLocaleMap[hostname];
}
return locale;
},
cacheUserLanguage(req, res, locale, options = {}) {
//todo
},
}

module.exports = new NextI18Next({
defaultLanguage: DEFAULT_LOCALE,
otherLanguages: supportedLangs,
localePath: path.resolve('./public/static/locales'),
customDetectors: [domainDetector],
detection: {
order: ['domain']
}
});
18 changes: 8 additions & 10 deletions src/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { I18nextProvider } from "react-i18next";
import i18n from "../i18n";
import { appWithTranslation } from "../i18n";
import { useRouter } from "next/router";
import Layout from "../components/Layout";
import "../styles.css";
import App from 'next/app'

function MyApp({ Component, pageProps }) {
const router = useRouter();
const { locale } = router.query;
console.log("App locale:", locale);
return (
<I18nextProvider i18n={i18n(locale)}>
<Layout>
<Component {...pageProps} />
</Layout>
</I18nextProvider>
<Layout>
<Component {...pageProps} />
</Layout>
);
}

export default MyApp;
MyApp.getInitialProps = async (appContext) => ({ ...await App.getInitialProps(appContext) });

export default appWithTranslation(MyApp);
22 changes: 0 additions & 22 deletions src/pages/_document.js

This file was deleted.

14 changes: 0 additions & 14 deletions src/pages/gip.js

This file was deleted.

6 changes: 4 additions & 2 deletions src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useTranslation } from "react-i18next";
import { useTranslation } from "../i18n";
import { useRouter } from "next/router";

export default function Home() {
Expand All @@ -8,5 +8,7 @@ export default function Home() {
}

Home.getInitialProps = async () => {
return {};
return {
namespacesRequired: ['common']
};
};