Skip to content

Commit

Permalink
feat: implement i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Poluhin committed May 30, 2023
1 parent f514bad commit c58aae6
Show file tree
Hide file tree
Showing 28 changed files with 263 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ node_modules
.editorconfig
.npmrc
.idea
.vscode
dist
lib
39 changes: 39 additions & 0 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 @@ -58,6 +58,7 @@
"webpack-cli": "^5.1.1"
},
"dependencies": {
"i18next": "^22.5.0",
"tsyringe": "^4.7.0"
}
}
9 changes: 9 additions & 0 deletions src/core/i18n/dictionary.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Lang } from './lang.enum';

export type Dictionary = {
[key in Lang]?: {
translation: {
[key: string]: string;
};
};
};
46 changes: 46 additions & 0 deletions src/core/i18n/dirctionary-loader.function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Dictionary } from './dictionary.interface';
import ar from '../../translations/ar.json';
import bg from '../../translations/bg.json';
import cs from '../../translations/cs.json';
import de from '../../translations/de.json';
import en from '../../translations/en.json';
import es from '../../translations/es.json';
import fr from '../../translations/fr.json';
import he from '../../translations/he.json';
import it from '../../translations/it.json';
import ja from '../../translations/ja.json';
import ko from '../../translations/ko.json';
import pl from '../../translations/pl.json';
import pt from '../../translations/pt.json';
import ro from '../../translations/ro.json';
import ru from '../../translations/ru.json';
import th from '../../translations/th.json';
import tr from '../../translations/tr.json';
import vi from '../../translations/vi.json';
import zhHans from '../../translations/zh_HANS.json';
import zhHant from '../../translations/zh_HANT.json';

export const loadDictionaries = (): Dictionary => {
return {
...ar,
...bg,
...cs,
...de,
...en,
...es,
...fr,
...he,
...it,
...ja,
...ko,
...pl,
...pt,
...ro,
...ru,
...th,
...tr,
...vi,
...zhHans,
...zhHant,
};
};
23 changes: 23 additions & 0 deletions src/core/i18n/lang.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export enum Lang {
AR = 'ar',
BG = 'bg',
CS = 'cs',
CN = 'cn',
DE = 'de',
EN = 'en',
ES = 'es',
FR = 'fr',
HE = 'he',
IT = 'it',
JA = 'ja',
KO = 'ko',
PL = 'pl',
PT = 'pt',
RO = 'ro',
RU = 'ru',
TH = 'th',
TR = 'tr',
VI = 'vi',
ZH_HANS = 'zh_HANS',
ZH_HANT = 'zh_HANT',
}
30 changes: 30 additions & 0 deletions src/core/i18n/localize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import i18next from 'i18next';
import { injectable } from 'tsyringe';
import { loadDictionaries } from './dirctionary-loader.function';
import { Lang } from './lang.enum';

@injectable()
export class Localize {
public async initDictionaries(): Promise<void> {
await i18next.init({
lng: Lang.EN,
fallbackLng: Lang.EN,
supportedLngs: Object.values(Lang),
debug: false,
resources: loadDictionaries(),
});
}

public translate(key: string): string {
return i18next.t(key);
}

public async changeLang(lang: Lang): Promise<void> {
// fallback for legacy locale
if (lang === Lang.CN) {
lang = Lang.ZH_HANS;
}

await i18next.changeLanguage(lang);
}
}
10 changes: 8 additions & 2 deletions src/features/headless-checkout/headless-checkout.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { injectable } from 'tsyringe';
import { PaymentMethod } from '../../core/payment-method.interface';
import { EventName } from '../../core/post-messages-client/event-name.enum';
import { Message } from '../../core/post-messages-client/message.interface';
import { PostMessagesClient } from '../../core/post-messages-client/post-messages-client';
import { Localize } from '../../core/i18n/localize';
import { getQuickMethodsHandler } from './post-messages-handlers/get-quick-methods.handler';
import { getRegularMethodsHandler } from './post-messages-handlers/get-regular-methods.handler';
import { setTokenHandler } from './post-messages-handlers/set-token.handler';
import { injectable } from 'tsyringe';
import { headlessCheckoutAppUrl } from './variables';

@injectable()
Expand All @@ -16,12 +17,17 @@ export class HeadlessCheckout {

public constructor(
private readonly window: Window,
private readonly postMessagesClient: PostMessagesClient
private readonly postMessagesClient: PostMessagesClient,
private readonly localize: Localize
) {}

public async init(environment: { isWebview: boolean }): Promise<void> {
this.isWebView = environment.isWebview;

await this.setupCoreIframe();

await this.localize.initDictionaries();

this.postMessagesClient.init(this.coreIframe, this.headlessAppUrl);
}

Expand Down
5 changes: 5 additions & 0 deletions src/translations/ar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ar": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/bg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"bg": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/cs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cs": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"de": {
"translation": {}
}
}
7 changes: 7 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"en": {
"translation": {
"hello": "Hello world!"
}
}
}
5 changes: 5 additions & 0 deletions src/translations/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"es": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"fr": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/he.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"he": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"it": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/ja.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ja": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/ko.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ko": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/pl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"pl": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/pt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"pt": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/ro.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ro": {
"translation": {}
}
}
7 changes: 7 additions & 0 deletions src/translations/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ru": {
"translation": {
"hello": "Привет мир!"
}
}
}
5 changes: 5 additions & 0 deletions src/translations/th.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"th": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/tr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tr": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/vi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"vi": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/zh_HANS.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"zh_HANS": {
"translation": {}
}
}
5 changes: 5 additions & 0 deletions src/translations/zh_HANT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"zh_HANT": {
"translation": {}
}
}

0 comments on commit c58aae6

Please sign in to comment.