{"_id":"@edqe/i18next-ssg","_rev":"1-f5b33ec512442fcfdc0f894238c53c3a","name":"@edqe/i18next-ssg","dist-tags":{"latest":"3.0.5"},"versions":{"3.0.4":{"name":"@edqe/i18next-ssg","version":"3.0.4","description":"i18n tool for next.js SSG","repository":{"type":"git","url":"git+ssh://git@github.com/Edqe14/i18next-ssg.git"},"publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"homepage":"https://github.com/Edqe14/i18next-ssg","main":"dist/commonjs/index.js","module":"dist/esm/index.js","types":"dist/types/index.d.ts","scripts":{"clean":"rm -rf dist","build:es":"tsc --module es6 --outDir dist/esm","build:cjs":"tsc --module commonjs --outDir dist/commonjs","build":"yarn clean & yarn build:es & yarn build:cjs","prepublishOnly":"yarn build","start":"node server.js"},"keywords":["yrobot","i18n","ssg","next.js"],"author":{"name":"yrobot"},"license":"ISC","dependencies":{},"devDependencies":{"@tsconfig/recommended":"^1.0.1","@types/node":"^18.7.17","@types/react":"^18.0.21","i18next":"^22.4.15","next":"^13.4.2","next-i18next":"^13.2.2","react-i18next":"^12.2.2","ts-node":"^10.9.1","typescript":"^4.8.3"},"peerDependencies":{"i18next":"^22.0.0","next":"^13.0.0","next-i18next":"^13.2.0","react":"^18.0.0","react-i18next":"^12.2.0"},"gitHead":"f421e79151bb5435a4d0d513b7c5fb0b7430c7e2","bugs":{"url":"https://github.com/Edqe14/i18next-ssg/issues"},"_id":"@edqe/i18next-ssg@3.0.4","_nodeVersion":"16.20.1","_npmVersion":"8.19.4","dist":{"integrity":"sha512-baVqoT4s3Yvqp8F3MBEOwlDDo8W/J/OTSGjxBofbE4jTKf+o35ySofed6lfzTPcB0LxCaeNR3bhK59+m5ltOUA==","shasum":"0db72707d0e9edd70ca0ec1b47f98d1c5f6cae08","tarball":"https://registry.npmjs.org/@edqe/i18next-ssg/-/i18next-ssg-3.0.4.tgz","fileCount":32,"unpackedSize":30068,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDfNw6c28oeqtXm31L5/mcZwnp+D+KW32fyeFAv/VRH6QIgfwHrePpDx9Y+G7w49iU1m6C1c3NtLfnuVAn8LGkwLyM="}]},"_npmUser":{"name":"edqe","email":"edqexyz@gmail.com"},"directories":{},"maintainers":[{"name":"edqe","email":"edqexyz@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/i18next-ssg_3.0.4_1691170520366_0.9796390769295382"},"_hasShrinkwrap":false},"3.0.5":{"name":"@edqe/i18next-ssg","version":"3.0.5","description":"i18n tool for next.js SSG","repository":{"type":"git","url":"git+ssh://git@github.com/Edqe14/i18next-ssg.git"},"publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"homepage":"https://github.com/Edqe14/i18next-ssg","main":"dist/commonjs/index.js","module":"dist/esm/index.js","types":"dist/types/index.d.ts","scripts":{"clean":"rm -rf dist","build:es":"tsc --module es6 --outDir dist/esm","build:cjs":"tsc --module commonjs --outDir dist/commonjs","build":"yarn clean & yarn build:es & yarn build:cjs","prepublishOnly":"yarn build","start":"node server.js"},"keywords":["yrobot","i18n","ssg","next.js"],"author":{"name":"yrobot"},"license":"MIT","dependencies":{},"devDependencies":{"@tsconfig/recommended":"^1.0.1","@types/node":"^18.7.17","@types/react":"^18.0.21","i18next":"^22.4.15","next":"^13.4.2","next-i18next":"^13.2.2","react-i18next":"^12.2.2","ts-node":"^10.9.1","typescript":"^4.8.3"},"peerDependencies":{"i18next":"^22.0.0","next":"^13.0.0","next-i18next":"^13.2.0","react":"^18.0.0","react-i18next":"^12.2.0"},"gitHead":"38ff7ea6a0b0ff607f39d2b7599068c8ddf53e8d","bugs":{"url":"https://github.com/Edqe14/i18next-ssg/issues"},"_id":"@edqe/i18next-ssg@3.0.5","_nodeVersion":"16.20.1","_npmVersion":"8.19.4","dist":{"integrity":"sha512-hP2pbWF2YHN/DCO4lC03qSXvVCXgfE4iG3ssLWULyvRILe7KOK0Z+03bSHdqUX2T0uLGkaaY+DZqpCJXdZ0bqQ==","shasum":"65eb1dc88f74de9ed7752ed6fa2cf821171c0fe5","tarball":"https://registry.npmjs.org/@edqe/i18next-ssg/-/i18next-ssg-3.0.5.tgz","fileCount":32,"unpackedSize":30079,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDuOITtBqf4exn7PUQ/KP2oUIQN+/zZuvmHYGZ9XBm6/wIgF7XUtGPvmEtkPpuRXVJknQ4GC0+oILH3qC4WOdm7g/E="}]},"_npmUser":{"name":"edqe","email":"edqexyz@gmail.com"},"directories":{},"maintainers":[{"name":"edqe","email":"edqexyz@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/i18next-ssg_3.0.5_1691171332219_0.16545889067935282"},"_hasShrinkwrap":false}},"time":{"created":"2023-08-04T17:35:20.276Z","3.0.4":"2023-08-04T17:35:20.594Z","modified":"2023-08-04T17:48:52.515Z","3.0.5":"2023-08-04T17:48:52.406Z"},"maintainers":[{"name":"edqe","email":"edqexyz@gmail.com"}],"description":"i18n tool for next.js SSG","homepage":"https://github.com/Edqe14/i18next-ssg","keywords":["yrobot","i18n","ssg","next.js"],"repository":{"type":"git","url":"git+ssh://git@github.com/Edqe14/i18next-ssg.git"},"author":{"name":"yrobot"},"bugs":{"url":"https://github.com/Edqe14/i18next-ssg/issues"},"license":"MIT","readme":"<p align=\"center\">\n  <a href=\"https://github.com/Yrobot/i18next-ssg\" target=\"_blank\" rel=\"noopener noreferrer\">\n    <img width=\"180\" src=\"https://images.yrobot.top/2022-11-19/i18next-ssg-10-33-34.svg\" alt=\"logo\">\n  </a>\n</p>\n<br/>\n<h2 align=\"center\">\n  <a href=\"https://github.com/Yrobot/i18next-ssg\">i18next-ssg</a>\n</h2>\n<p align=\"center\">\n  The i18n tool for next.js ssg project.\n</p>\n<p align=\"center\">\n  <a href=\"https://www.npmjs.com/package/i18next-ssg\"><img src=\"https://img.shields.io/npm/v/i18next-ssg.svg\" alt=\"npm package\"></a>\n</p>\n<br/>\n\n## Demos\n\n- [Next.js SSG](https://codesandbox.io/p/github/yrobot-demo/i18next-ssg/master)\n  - [Demo Repo](https://github.com/yrobot-demo/i18next-ssg)\n\n## How To Start\n\n### 1. Install `i18next-ssg`\n\n```\nyarn add i18next-ssg next-i18next react-i18next i18next\n```\n\n### 2. Project Setup\n\nFirst, create a `next-i18next.config.js` file in the root of your project.\n\n`next-i18next.config.js`\n\n```js\nmodule.exports = {\n  i18n: {\n    defaultLocale: \"en\", // the default language\n    locales: [\"en\", \"zh\"], // language list\n  },\n};\n```\n\nAnd You have to expose the `i18n` config as the next.js Environment Variable `NEXT_PUBLIC_I18N`.\n\n`next.config.js`\n\n```js\nconst { i18n } = require(\"./next-i18next.config\");\n\nmodule.exports = {\n  env: {\n    NEXT_PUBLIC_I18N: i18n,\n  },\n};\n```\n\n### 3. Add locales json files to your Next.js project\n\n```\n- public\n  - locales\n    - zh\n      - common.json\n    - en\n      - common.json\n    - ... other languages\n```\n\n`public/locales/en/common.json`\n\n```json\n{\n  \"title\": \"English Title\"\n}\n```\n\nIf you want to structure your translations/namespaces in a custom way, you will need to pass modified `localePath` and `localeStructure` values into `next-i18next.config.js`. Check detail here https://github.com/i18next/next-i18next#options\n\n### 4. Wrap the app\n\n`pages/_app.tsx`\n\nadd `appWithTranslation` logic\n\n```tsx\nimport { appWithTranslation } from \"i18next-ssg\";\n\nconst App = ({ Component, pageProps }) => <Component {...pageProps} />;\n\nexport default appWithTranslation(App);\n```\n\n### 5. Create `pages/[locale]` directory and Add pages under it\n\n```\npages\n  - [locale]\n    - index.tsx\n    - second.tsx\n    - third.tsx\n```\n\n> `/second/index.tsx` and `/second.tsx` both work fine for `/second`\n\nAdd locale inject logic in every page, for example:  \n`pages/[locale]/index.tsx`\n\n```tsx\nimport { makeStaticProps, getStaticPaths } from \"i18next-ssg/server\";\n\nexport default Page(){\n  return <div>Page</div>\n}\n\nconst getStaticProps = makeStaticProps([\"common\"]);\nexport { getStaticPaths, getStaticProps };\n```\n\n### 6. use i18n inside the page\n\nfor example:\n\n```tsx\nimport { useTranslation } from \"i18next-ssg\";\n\nexport default function Title() {\n  const { t } = useTranslation(\"common\");\n  return <div>{t(\"title\")}</div>;\n}\n```\n\n### 7. Add root redirect logic [optional]\n\nFirst, create `[[...paths]].tsx` under the `pages` folder (i18next-ssg will auto generate the root pages based on the pages you created under `[locale]`)\n\n`pages/[[...paths]].tsx`\n\n> copy pause the code directly without any changes, it will works fine for your project.\n\n```tsx\nexport { getStaticProps, getStaticPaths } from \"i18next-ssg/Redirect\";\nimport { useRootPathRedirect } from \"i18next-ssg\";\n\nexport default function Page() {\n  useRootPathRedirect();\n  return <div>Redirecting...</div>;\n}\n```\n\nThen, the auto redirect logic works like this:\n\n- / => /[locale]\n- /second => /[locale]/second\n- /third => /[locale]/third\n\nThe `[locale]` value based on `getLocale`, which strategy works follow this:\n\n`[url locale]` > `[user set locale in localStorage]` > `[locale detect from browser navigator]`\n\n---\n\nAfter all these steps and run `yarn build` (`next build && next export -o build`), you will get output files below:\n\n```\n- build\n  - locales\n    - ... your locale json files\n  - index.html\n  - second.html\n  - third.html\n  - zh.html\n  - en.html\n  - en\n    - second.html\n    - third.html\n  - zh\n    - second.html\n    - third.html\n```\n\n## Other Tricks\n\n### get your i18n config quickly\n\nthis work fine on both server and browser.\n\n```tsx\nimport { config } from \"i18next-ssg\";\n\nconsole.log(config.locales);\nconsole.log(config.defaultLocale);\n```\n\n### All functions exported from `next-i18next` or `react-i18next`\n\n```tsx\nimport {\n  appWithTranslation,\n  serverSideTranslations,\n  i18n,\n  withTranslation,\n  I18nContext,\n  Trans,\n  Translation,\n  useTranslation,\n} from \"i18next-ssg\";\n```\n\n### locale route logic\n\n#### `I18NLink`\n\n```tsx\nimport { I18NLink } from \"i18next-ssg\";\nexport default function Pages() {\n  return <I18NLink href=\"/sansa\">Sansa</I18NLink>; // /sansa => /[locale]/sansa\n}\n```\n\n#### `localize`\n\n```tsx\nimport { useRouter } from \"next/router\";\nimport { localize } from \"i18next-ssg\";\nexport default function Pages() {\n  const router = useRouter();\n  return (\n    <span\n      onClick={() => {\n        router.push(localize(\"/sansa\")); // /sansa => /[locale]/sansa\n      }}\n    >\n      Sansa\n    </span>\n  );\n}\n```\n\n### locale switch logic\n\n```tsx\nimport Link from \"next/link\";\nimport { useLocaleSwitcher, setUserLocale } from \"i18next-ssg\";\n\nconst localeMap: Record<Locale, string> = {\n  en: \"English\",\n  zh: \"中文\",\n};\n\nfunction LocaleSwitcher() {\n  const {\n    label, // current locale label\n    options, // the option list of other locales\n  } = useLocaleSwitcher({ localeMap });\n  return (\n    <div>\n      <label>{label}</label>\n      <ul>\n        {options.map(({ label, path, locale }) => (\n          <li key={path}>\n            <Link href={path}>\n              <span\n                onClick={() => {\n                  setUserLocale(locale);\n                }}\n              >\n                {label}\n              </span>\n            </Link>\n          </li>\n        ))}\n      </ul>\n    </div>\n  );\n}\n\nexport default LocaleSwitcher;\n```\n","readmeFilename":"README.md"}