Skip to content

Commit

Permalink
Merge pull request #2 from xsolla/PAYMENTS-18897
Browse files Browse the repository at this point in the history
feat(PAYMENTS-18897): infrastructure for translations
  • Loading branch information
ekireevxs authored Apr 10, 2024
2 parents 545eb32 + 1c0d614 commit 65aa15f
Show file tree
Hide file tree
Showing 22 changed files with 278 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ module.exports = {
tsconfigRootDir: __dirname,
},
settings: {
react: {
version: "18",
},
'import/resolver': {
alias: {
// Resolve Vite public path like: import logo from "/brand-logo_xsolla.png"
Expand All @@ -27,6 +30,7 @@ module.exports = {
},
rules: {
'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
'react/react-in-jsx-scope': ['off'],
},
overrides: [
{
Expand Down
145 changes: 137 additions & 8 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-intl": "^6.6.5",
"react-router-dom": "^6.20.0",
"styled-components": "^5.2.0",
"zod": "^3.22.4"
Expand Down
8 changes: 6 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import Layout from './layout.tsx';
import { NoMatch } from './routes/not-found';
import { Home } from './routes/home';
import { LocalizationContextProvider } from './app/contexts/localization-context/localization-context-provider.tsx';

const router = createBrowserRouter([
{
Expand All @@ -22,5 +22,9 @@ const router = createBrowserRouter([
]);

export default function App() {
return <RouterProvider router={router} />;
return (
<LocalizationContextProvider>
<RouterProvider router={router} />
</LocalizationContextProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { FC, ReactNode, useEffect, useState } from 'react';
import { LocalizationContext } from './localization-context.ts';
import { Languages } from '../../localization/languages.enum.ts';
import { loadMessages } from '../../localization/load-localization.ts';
import { IntlProvider } from 'react-intl';

export const LocalizationContextProvider: FC<{ children: ReactNode }> = ({ children }) => {
const [currentLocale, setCurrentLocale] = useState<Languages>(Languages.EN);
const [messages, setMessages] = useState(null);

useEffect(() => {
loadMessages(currentLocale).then((data) => setMessages(data.default));
}, [currentLocale]);

return (
<LocalizationContext.Provider value={{ currentLocale, setCurrentLocale }}>
<IntlProvider locale={currentLocale} messages={messages}>
{messages && children}
</IntlProvider>
</LocalizationContext.Provider>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Languages } from '../../localization/languages.enum.ts';
import { Dispatch, SetStateAction } from 'react';

export interface ILocalizationContext {
currentLocale: Languages;
setCurrentLocale: Dispatch<SetStateAction<Languages>>;
}
6 changes: 6 additions & 0 deletions src/app/contexts/localization-context/localization-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
import { ILocalizationContext } from './localization-context.interface.ts';

export const LocalizationContext = React.createContext<ILocalizationContext>(
{} as ILocalizationContext,
);
11 changes: 11 additions & 0 deletions src/app/localization/languages.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export enum Languages {
CN = 'cn',
DE = 'de',
EN = 'en',
ES = 'es',
FR = 'fr',
JA = 'ja',
KO = 'ko',
PT = 'pt',
RU = 'ru',
}
28 changes: 28 additions & 0 deletions src/app/localization/load-localization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Languages } from './languages.enum.ts';

export async function loadMessages(
locale: Languages,
): Promise<{ default: Record<string, string> }> {
switch (locale) {
case Languages.CN:
return import(`@translations/cn.json`);
case Languages.DE:
return import(`@translations/de.json`);
case Languages.EN:
return import(`@translations/en.json`);
case Languages.ES:
return import(`@translations/es.json`);
case Languages.FR:
return import(`@translations/fr.json`);
case Languages.JA:
return import(`@translations/ja.json`);
case Languages.KO:
return import(`@translations/ko.json`);
case Languages.PT:
return import(`@translations/pt.json`);
case Languages.RU:
return import(`@translations/ru.json`);
default:
return import(`@translations/en.json`);
}
}
2 changes: 2 additions & 0 deletions src/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { Link, Outlet } from 'react-router-dom';
import { FormattedMessage } from 'react-intl';

export default function Layout() {
return (
Expand All @@ -8,6 +9,7 @@ export default function Layout() {
<ul>
<li>
<Link to='/'>Home</Link>
<FormattedMessage id={'hello-world'} defaultMessage={'Hello world'}></FormattedMessage>
</li>
</ul>
</nav>
Expand Down
3 changes: 3 additions & 0 deletions src/translations/cn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello-world": "Hello world"
}
3 changes: 3 additions & 0 deletions src/translations/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello-world": "Hello world"
}
3 changes: 3 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello-world": "Hello world"
}
3 changes: 3 additions & 0 deletions src/translations/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello-world": "Hello world"
}
3 changes: 3 additions & 0 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello-world": "Hello world"
}
3 changes: 3 additions & 0 deletions src/translations/ja.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello-world": "Hello world"
}
3 changes: 3 additions & 0 deletions src/translations/ko.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello-world": "Hello world"
}
3 changes: 3 additions & 0 deletions src/translations/pt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello-world": "Hello world"
}
3 changes: 3 additions & 0 deletions src/translations/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello-world": "Привет мир"
}
Loading

0 comments on commit 65aa15f

Please sign in to comment.