{title}
26 |{desc}
27 | 28 |
29 | Trans component
31 |
├── netlify.toml
├── astro.config.js
├── src
├── locales
│ ├── index.js
│ ├── en
│ │ └── index.json
│ ├── es
│ │ └── index.json
│ └── fr
│ │ └── index.json
├── components
│ └── Trans.jsx
├── pages
│ └── index.astro
└── utils
│ └── i18n.js
├── .gitignore
├── public
└── global.css
├── i18next-parser.config.js
├── package.json
└── README.md
/netlify.toml:
--------------------------------------------------------------------------------
1 | [build]
2 | command = "yarn build"
3 | publish = "dist/"
--------------------------------------------------------------------------------
/astro.config.js:
--------------------------------------------------------------------------------
1 | export default {
2 | renderers: ["@astrojs/renderer-react"],
3 | };
4 |
--------------------------------------------------------------------------------
/src/locales/index.js:
--------------------------------------------------------------------------------
1 | export { default as en } from "./en/index.json";
2 | export { default as es } from "./es/index.json";
3 | export { default as fr } from "./fr/index.json";
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | package-lock.json
4 |
5 | dist
6 | .netlify
7 |
8 | npm-debug.log*
9 | yarn-debug.log*
10 | yarn-error.log*
11 |
12 | .idea/
13 | .vscode/
--------------------------------------------------------------------------------
/src/locales/en/index.json:
--------------------------------------------------------------------------------
1 | {
2 | "Astro with i18next example": "Astro with i18next example",
3 | "An opinionated approach to translation with Astro and i18next.": "An opinionated approach to translation with Astro and i18next.",
4 | "transExample": "This is an example of using the Trans component"
5 | }
6 |
--------------------------------------------------------------------------------
/src/locales/es/index.json:
--------------------------------------------------------------------------------
1 | {
2 | "Astro with i18next example": "Ejemplo de Astro con i18next",
3 | "An opinionated approach to translation with Astro and i18next.": "Un enfoque obstinado de la traducción con Astro e i18next.",
4 | "transExample": "Este es un ejemplo de uso del Trans componente."
5 | }
6 |
--------------------------------------------------------------------------------
/src/locales/fr/index.json:
--------------------------------------------------------------------------------
1 | {
2 | "Astro with i18next example": "Astro avec i18next exemple",
3 | "An opinionated approach to translation with Astro and i18next.": "Une approche avisée de la traduction avec Astro et i18next.",
4 | "transExample": "Ceci est un exemple d'utilisation du composant Trans."
5 | }
6 |
--------------------------------------------------------------------------------
/public/global.css:
--------------------------------------------------------------------------------
1 | * {
2 | box-sizing: border-box;
3 | }
4 |
5 | body {
6 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
7 | Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
8 | }
9 |
10 | main {
11 | margin: 0 auto;
12 | max-width: 50em;
13 | width: 95%;
14 | }
15 |
16 | nav {
17 | display: flex;
18 | flex-wrap: wrap;
19 | gap: 1rem;
20 | }
21 |
--------------------------------------------------------------------------------
/i18next-parser.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | input: ["src/**/*.{astro,jsx,js}", "!**/node_modules/**", "!public/**"],
3 | output: "src/locales/$LOCALE/index.json",
4 | defaultNamespace: "translation",
5 | keySeparator: false,
6 | namespaceSeparator: false,
7 | locales: ["en", "es", "fr"],
8 | useKeysAsDefaultValue: true,
9 | // We'll likely want to turn this off once our setup is a better place
10 | keepRemoved: true,
11 | };
12 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "astro-example-i18next",
3 | "version": "1.0.0",
4 | "description": "An opinionated approach to translation with Astro and i18next",
5 | "main": "index.js",
6 | "scripts": {
7 | "start": "astro dev",
8 | "build": "astro build",
9 | "extract-translations": "i18next"
10 | },
11 | "author": "me@tylergaw.com",
12 | "license": "ISC",
13 | "devDependencies": {
14 | "astro": "0.20.4",
15 | "i18next": "20.6.0",
16 | "i18next-parser": "4.4.0",
17 | "react-i18next": "11.11.4"
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/components/Trans.jsx:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import { Trans as TransOriginal, useTranslation } from "react-i18next";
3 |
4 | /**
5 | * This is a wrapper component for react-i18next Trans. We wrap it so we can
6 | * access the useTranslation hook, then we can import this component into
7 | * .astro components for use in translating blocks of text/elements.
8 | */
9 | const Trans = (props) => {
10 | const { t } = useTranslation();
11 |
12 | return (
13 |
{desc}
27 | 28 |
29 | Trans component
31 |