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

Feature/pxweb2-41 install i18next #61

Merged
merged 7 commits 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
6 changes: 6 additions & 0 deletions apps/pxweb2/public/locales/ar/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"app_title": "مرحبًا بك في PxWeb",
"main": {
"header": "مرحبا بكم في التطبيق!"
}
}
10 changes: 10 additions & 0 deletions apps/pxweb2/public/locales/en/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"app_title": "Welcome to PxWeb",
"main": {
"header": "Welcome to the app!"
},
"date": {
"simple_date": "{{value, datetime}}",
"simple_date_with_time": "{{value, datetime(year: 'numeric'; month: 'numeric'; day: 'numeric'; hour: 'numeric'; minute: 'numeric')}}"
}
}
6 changes: 6 additions & 0 deletions apps/pxweb2/public/locales/no/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"app_title": "Velkommen til PxWeb",
"main": {
"header": "Velkommen til appen!"
}
}
6 changes: 6 additions & 0 deletions apps/pxweb2/public/locales/sv/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"app_title": "Välkommen till PxWeb",
"main": {
"header": "Välkommen till appen!"
}
}
2 changes: 1 addition & 1 deletion apps/pxweb2/public/theme/variables.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Do not edit directly
* Generated on Tue, 06 Feb 2024 11:35:57 GMT
* Generated on Thu, 29 Feb 2024 09:16:51 GMT
*/

:root {
Expand Down
45 changes: 40 additions & 5 deletions apps/pxweb2/src/app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import styles from './app.module.scss';
import { useTranslation } from 'react-i18next';

import {
Button,
Expand All @@ -9,6 +8,7 @@ import {
Ingress,
Label,
} from '@pxweb2/pxweb2-ui';
import useLocalizeDocumentAttributes from '../i18n/useLocalizeDocumentAttributes';

function test(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) {
event.preventDefault();
Expand All @@ -19,15 +19,40 @@ function testSubmit() {
}

export function App() {
const { t, i18n } = useTranslation();

const locales = {
en: { title: 'English' },
no: { title: 'Norsk' },
sv: { title: 'Svenska' },
ar: { title: 'العربية' },
};

useLocalizeDocumentAttributes();

return (
<>
<ul>
{Object.keys(locales).map((locale) => (
<li key={locale}>
<button
style={{
fontWeight:
i18n.resolvedLanguage === locale ? 'bold' : 'normal',
}}
type="submit"
onClick={() => i18n.changeLanguage(locale)}
>
{locales[locale as keyof typeof locales].title}
</button>
</li>
))}
</ul>
<Heading level="1" size="xlarge">
Welcome to PxWeb 2.0
</Heading>
<br />
<Ingress spacing>
Ingress: This page will display various components
</Ingress>
<Ingress spacing>{t('main.header')}</Ingress>
<BodyShort size="medium" spacing align="start" weight="regular">
BodyShort: This component will be used for text with not more than 80
characters.
Expand Down Expand Up @@ -78,6 +103,16 @@ export function App() {
<Button variant="secondary" icon="FloppyDisk" onClick={test}></Button>
&nbsp;
<Button variant="secondary" icon="Heart" onClick={test}></Button>
<p>
{t('date.simple_date', {
value: new Date('2024-01-25'),
})}
</p>
<p>
{t('date.simple_date_with_time', {
value: new Date('2024-01-25 12:34:56'),
})}
</p>
</>
);
}
Expand Down
27 changes: 27 additions & 0 deletions apps/pxweb2/src/i18n/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import i18n from 'i18next';
import HttpApi from 'i18next-http-backend';
import { initReactI18next } from 'react-i18next';

i18n
.use(HttpApi)
.use(initReactI18next)
.init({
backend: {
requestOptions: {
// Do not cache the response from the server. This is needed because site administrators
// may want to change the translations without having to wait for the cache to expire.
cache: 'no-store',
},
},
lng: 'en',
fallbackLng: 'en',
// Explicitly tell i18next our
// supported locales.
supportedLngs: ['en', 'no', 'sv', 'ar'],
debug: true,
interpolation: {
escapeValue: false,
},
});

export default i18n;
16 changes: 16 additions & 0 deletions apps/pxweb2/src/i18n/useLocalizeDocumentAttributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useEffect } from 'react';
import { useTranslation } from 'react-i18next';

export default function useLocalizeDocumentAttributes() {
const { t, i18n } = useTranslation();

useEffect(() => {
if (i18n.resolvedLanguage) {
document.documentElement.lang = i18n.resolvedLanguage;
document.documentElement.dir = i18n.dir(i18n.resolvedLanguage);
}

document.title = t('app_title');

}, [i18n, i18n.resolvedLanguage, t]);
}
7 changes: 5 additions & 2 deletions apps/pxweb2/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { StrictMode } from 'react';
import { StrictMode, Suspense } from 'react';
import * as ReactDOM from 'react-dom/client';

import App from './app/app';
import './i18n/config';

const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<StrictMode>
<App />
<Suspense fallback={<div>Loading...</div>}>
<App />
</Suspense>
</StrictMode>
);
92 changes: 82 additions & 10 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
"dependencies": {
"@swc/helpers": "^0.5.6",
"clsx": "^2.1.0",
"i18next": "^23.10.0",
"i18next-http-backend": "^2.5.0",
"react": "18.2.0",
"react-dom": "18.2.0"
"react-dom": "18.2.0",
"react-i18next": "^14.0.5"
},
"devDependencies": {
"@babel/core": "^7.23.9",
Expand Down