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

i18n translations setup #3

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
90 changes: 81 additions & 9 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
},
"dependencies": {
"@etchteam/diamond-ui": "^1.0.3",
"i18next": "^23.7.18",
"i18next-http-backend": "^2.4.2",
"lit": "^3.1.1",
"preact": "^10.19.3",
"react-i18next": "^14.0.1",
"react-router-dom": "^6.21.3"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions public/translations/cy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"start": {
"title": "Dod o hyd i leoedd i ailgylchu."
}
}
5 changes: 5 additions & 0 deletions public/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"start": {
"title": "Find places to recycle."
}
}
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LitElement, css } from 'lit';
import { customElement } from 'lit/decorators.js';
import { render as preactRender } from 'preact';

import Entrypoint from './app/Entrypoint';
import Entrypoint from './pages/Entrypoint';
import { diamondUi } from './styles/diamond-ui';
import { variables } from './styles/variables';

Expand Down
24 changes: 24 additions & 0 deletions src/lib/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import i18n from 'i18next';

Check warning on line 1 in src/lib/i18n.ts

View workflow job for this annotation

GitHub Actions / lint

Using exported name 'i18n' as identifier for default export
import HttpBackend, { HttpBackendOptions } from 'i18next-http-backend';
import { initReactI18next } from 'react-i18next';

i18n

Check warning on line 5 in src/lib/i18n.ts

View workflow job for this annotation

GitHub Actions / lint

Caution: `i18n` also has a named export `use`. Check if you meant to write `import {use} from 'i18next'` instead
// load translation using http -> see /public/translations
// https://github.com/i18next/i18next-http-backend
.use(HttpBackend)
// pass the i18n instance to react-i18next.
.use(initReactI18next)
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init<HttpBackendOptions>({
fallbackLng: 'en',
debug: true,
backend: {
loadPath: '/translations/{{lng}}.json',
},
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
},
});

export default i18n;
File renamed without changes.
10 changes: 9 additions & 1 deletion src/app/Entrypoint.tsx → src/pages/Entrypoint.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Suspense } from 'preact/compat';
import {
createMemoryRouter,
createRoutesFromElements,
Route,
RouterProvider,
} from 'react-router-dom';

import '../lib/i18n';

import NotFound from './404.js';
import StartPage from './Start.js';

Expand All @@ -19,7 +22,12 @@ const router = createMemoryRouter(
* - Load up the router
* - Setup the start page routes
* - Lazily register sub routes
* - Initialise i18n (using suspense to wait for them to load in)
*/
export default function Entrypoint() {
return <RouterProvider router={router} />;
return (
<Suspense fallback={<h2>loading...</h2>}>
<RouterProvider router={router} />
</Suspense>
);
}
6 changes: 5 additions & 1 deletion src/app/Start.tsx → src/pages/Start.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { useTranslation } from 'react-i18next';

export default function StartPage() {
const { t } = useTranslation();

return (
<locator-layout>
<header slot="header">
<locator-logo>Recycling Locator</locator-logo>
</header>
<div slot="main">
<h2>Find places to recycle</h2>
<h2>{t('start.title')}</h2>
<form>
<label htmlFor="location">Where are you?</label>
<input type="text" name="location" id="location" />
Expand Down
Loading