Skip to content

Commit

Permalink
Initial version of localization
Browse files Browse the repository at this point in the history
  • Loading branch information
SalamaGofore committed May 16, 2024
1 parent 3476aac commit c4f53ef
Show file tree
Hide file tree
Showing 7 changed files with 899 additions and 2,009 deletions.
2,835 changes: 830 additions & 2,005 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
"@tanstack/react-query": "^5.29.2",
"@tanstack/react-query-devtools": "^5.29.2",
"cookie": "^0.6.0",
"i18next": "^23.11.4",
"i18next-http-backend": "^2.5.1",
"next": "^14.2.2",
"nuqs": "^1.17.2",
"react": "^18",
"react-dom": "^18"
"react-dom": "^18",
"react-i18next": "^14.1.1"
},
"devDependencies": {
"@axe-core/playwright": "^4.9.0",
Expand Down
5 changes: 5 additions & 0 deletions src/app/haku/[oid]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use client';

import { useTranslation } from 'react-i18next';

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

return (
<div style={{ alignSelf: 'center', width: '70%', padding: '1rem 2rem' }}>
<h2>Valitse hakukohde</h2>
<p>{t('title')}</p>
</div>
);
}
2 changes: 2 additions & 0 deletions src/app/lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface Configuration {
kooditUrl: string;
koutaInternalLogin: string;
asiointiKieliUrl: string;
lokalisaatioUrl: string;
}

export const configuration: Configuration = {
Expand All @@ -27,4 +28,5 @@ export const configuration: Configuration = {
kooditUrl: `${DOMAIN}/koodisto-service/rest/codeelement/codes/`,
koutaInternalLogin: `${DOMAIN}/kouta-internal/auth/login`,
asiointiKieliUrl: `${DOMAIN}/oppijanumerorekisteri-service/henkilo/current/asiointiKieli`,
lokalisaatioUrl: `${DOMAIN}/lokalisointi/cxf/rest/v1/localisation?category=valintojen-toteuttaminen&locale=`,
};
21 changes: 21 additions & 0 deletions src/app/lib/localization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use server';

import { configuration } from './configuration';
import { client } from './http-client';
import { Language } from './common';

export const getTranslations = async (lng: Language) => {
const translations = {};
const { data } = await client.get(`${configuration.lokalisaatioUrl}${lng}`);

console.log(data);

for (const translation of data) {
translations[translation.key] = translation.value;

Check failure on line 14 in src/app/lib/localization.ts

View workflow job for this annotation

GitHub Actions / lint

Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}'.
}

console.log(translations);
console.log('TRANSLATIONS ' + lng.toString());

return translations;
};
29 changes: 29 additions & 0 deletions src/app/lib/translations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use client';

import HttpBackend from 'i18next-http-backend';
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import { getTranslations } from './localization';

export const createLocalization = () => {
i18n
.use(HttpBackend)
.use(initReactI18next)
.init({
debug: true,
fallbackLng: 'fi',
preload: ['fi', 'sv', 'en'],
lng: 'fi',
backend: {
loadPath: '{{lng}}',
request: (options, url, payload, callback) => {

Check failure on line 19 in src/app/lib/translations.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter 'options' implicitly has an 'any' type.

Check failure on line 19 in src/app/lib/translations.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter 'url' implicitly has an 'any' type.

Check failure on line 19 in src/app/lib/translations.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter 'payload' implicitly has an 'any' type.

Check failure on line 19 in src/app/lib/translations.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter 'callback' implicitly has an 'any' type.
getTranslations(url)
.then((data) => {
callback(null, { status: 200, data });
})
.catch(() => callback({ status: 404 }));
},
},
});
return i18n;
};
11 changes: 8 additions & 3 deletions src/app/wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
'use client';
import { I18nextProvider } from 'react-i18next';
import { FullSpinner } from './components/full-spinner';
import { useAsiointiKieli } from './hooks/useAsiointiKieli';
import { useAsiointiKieli } from './lib/hooks/useAsiointiKieli';

Check failure on line 4 in src/app/wrapper.tsx

View workflow job for this annotation

GitHub Actions / lint

Cannot find module './lib/hooks/useAsiointiKieli' or its corresponding type declarations.
import { createLocalization } from './lib/translations';

const localization = createLocalization();

export default function Wrapper({ children }: { children: React.ReactNode }) {
const { isLoading, isError, error } = useAsiointiKieli();
const { isLoading, isError, error, data } = useAsiointiKieli();

switch (true) {
case isLoading:
return <FullSpinner />;
case isError:
throw error;
default:
return children;
localization.changeLanguage(data?.data ?? 'fi');
return <I18nextProvider i18n={localization}>{children}</I18nextProvider>;
}
}

0 comments on commit c4f53ef

Please sign in to comment.