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

Add typesafety/intellisense to translation keys #64

Merged
merged 1 commit into from
Mar 5, 2024
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
3 changes: 2 additions & 1 deletion apps/pxweb2/public/locales/ar/translation.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"app_title": "مرحبًا بك في PxWeb",
"main": {
"header": "مرحبا بكم في التطبيق!"
"header": "مرحبا بكم في التطبيق!",
"welcome_trans_test": "مرحبًا بك في <1>التطبيق</1> لـ PxWeb 2.0!"
}
}
3 changes: 2 additions & 1 deletion apps/pxweb2/public/locales/en/translation.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"app_title": "Welcome to PxWeb",
"main": {
"header": "Welcome to the app!"
"header": "Welcome to the app!",
"welcome_trans_test": "Welcome to the <1>app</1> for PxWeb 2.0!"
},
"date": {
"simple_date": "{{value, datetime}}",
Expand Down
3 changes: 2 additions & 1 deletion apps/pxweb2/public/locales/no/translation.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"app_title": "Velkommen til PxWeb",
"main": {
"header": "Velkommen til appen!"
"header": "Velkommen til appen!",
"welcome_trans_test": "Velkommen til <1>appen</1> for PxWeb 2.0!"
}
}
3 changes: 2 additions & 1 deletion apps/pxweb2/public/locales/sv/translation.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"app_title": "Välkommen till PxWeb",
"main": {
"header": "Välkommen till appen!"
"header": "Välkommen till appen!",
"welcome_trans_test": "Välkommen till <1>appen</1> för PxWeb 2.0!"
}
}
9 changes: 9 additions & 0 deletions apps/pxweb2/src/@types/i18next.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Resources from './resources';
import { defaultNS } from '../i18n/config';

declare module 'i18next' {
interface CustomTypeOptions {
defaultNS: typeof defaultNS;
resources: Resources;
}
}
15 changes: 15 additions & 0 deletions apps/pxweb2/src/@types/resources.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
interface Resources {
"translation": {
"app_title": "Welcome to PxWeb",
"main": {
"header": "Welcome to the app!",
"welcome_trans_test": "Welcome to the <1>app</1> for PxWeb 2.0!"
},
"date": {
"simple_date": "{{value, datetime}}",
"simple_date_with_time": "{{value, datetime(year: 'numeric'; month: 'numeric'; day: 'numeric'; hour: 'numeric'; minute: 'numeric')}}"
}
}
}

export default Resources;
7 changes: 6 additions & 1 deletion apps/pxweb2/src/app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useTranslation } from 'react-i18next';
import { useTranslation, Trans } from 'react-i18next';

import {
Button,
Expand Down Expand Up @@ -113,6 +113,11 @@ export function App() {
value: new Date('2024-01-25 12:34:56'),
})}
</p>
<p>
<Trans i18nKey="main.welcome_trans_test">
"Welcome to the <b>app</b> for PxWeb 2.0!"
</Trans>
</p>
</>
);
}
Expand Down
3 changes: 3 additions & 0 deletions apps/pxweb2/src/i18n/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import i18n from 'i18next';
import HttpApi from 'i18next-http-backend';
import { initReactI18next } from 'react-i18next';

export const defaultNS = 'translation';

i18n
.use(HttpApi)
.use(initReactI18next)
Expand All @@ -15,6 +17,7 @@ i18n
},
lng: 'en',
fallbackLng: 'en',
defaultNS,
// Explicitly tell i18next our
// supported locales.
supportedLngs: ['en', 'no', 'sv', 'ar'],
Expand Down
3 changes: 2 additions & 1 deletion apps/pxweb2/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"types": ["vite/client", "vitest"]
"types": ["vite/client", "vitest"],
"resolveJsonModule": true,
},
"files": [],
"include": [],
Expand Down
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build": "nx run pxweb2:build && nx run pxweb2-ui:build-storybook",
"start-storybook": "npx nx run pxweb2-ui:storybook",
"build-storybook": "npx nx run pxweb2-ui:build-storybook",
"build-style-dictionary": "node libs/pxweb2-ui/style-dictionary/build.js"
"build-style-dictionary": "node libs/pxweb2-ui/style-dictionary/build.js",
"build-i18n-interface": "npx i18next-resources-for-ts interface -i ./apps/pxweb2/public/locales/en -o ./apps/pxweb2/src/@types/resources.d.ts"
},
"private": true,
"dependencies": {
Expand Down Expand Up @@ -62,6 +63,7 @@
"eslint-plugin-jsx-a11y": "6.7.1",
"eslint-plugin-react": "7.32.2",
"eslint-plugin-react-hooks": "4.6.0",
"i18next-resources-for-ts": "^1.5.0",
"jsdom": "~22.1.0",
"nx": "18.0.5",
"prettier": "^2.8.8",
Expand Down