Skip to content
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
32 changes: 24 additions & 8 deletions src/utils/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
import * as ts from 'typescript';

const convertArrayItemToType = async (item: any): Promise<ts.TypeNode> => {
if (Array.isArray(item)) {
const nestedArrayItemTypes = await Promise.all(
item.map((nestedItem) => convertArrayItemToType(nestedItem)),
);
return ts.factory.createTupleTypeNode(nestedArrayItemTypes);
}
if (typeof item === 'object') {
return ts.factory.createTypeLiteralNode(
await convertObjectToTypeDefinition(item),
);
}
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
};

export const convertObjectToTypeDefinition = async (
object: any,
): Promise<ts.TypeElement[]> => {
switch (typeof object) {
case 'object':
return Promise.all(
Object.keys(object).map(async (key) => {
if (typeof object[key] === 'string') {
const value = object[key];

if (typeof value === 'string') {
return ts.factory.createPropertySignature(
undefined,
ts.factory.createStringLiteral(key),
undefined,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
);
}
if (Array.isArray(object[key])) {
if (Array.isArray(value)) {
const arrayItemTypes = await Promise.all(
value.map((item) => convertArrayItemToType(item)),
);
return ts.factory.createPropertySignature(
undefined,
ts.factory.createStringLiteral(key),
undefined,
ts.factory.createTupleTypeNode(
Array(object[key].length).fill(
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
),
),
ts.factory.createTupleTypeNode(arrayItemTypes),
);
}
return ts.factory.createPropertySignature(
undefined,
ts.factory.createStringLiteral(key),
undefined,
ts.factory.createTypeLiteralNode(
await convertObjectToTypeDefinition(object[key]),
await convertObjectToTypeDefinition(value),
),
);
}),
Expand Down
14 changes: 14 additions & 0 deletions tests/generated/i18n.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ export type I18nTranslations = {
string,
string
];
"OBJECTS_ARRAY": [
{
"instructions": string;
},
{
"nestedArrayInstructions": [
[
{
"instructions": string;
}
]
];
}
];
"cat": string;
"ONLY_EN_KEY": string;
"cat_name": string;
Expand Down
30 changes: 30 additions & 0 deletions tests/i18n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,36 @@ describe('i18n module', () => {
expect(i18nService.translate('test.ARRAY.2', { lang: 'nl' })).toBe('DRIE');
});

it('i18n service should return objects array translation', () => {
expect(
i18nService.translate('test.OBJECTS_ARRAY.0.instructions', {
lang: 'en',
}),
).toBe('Please follow the link');
expect(
i18nService.translate(
'test.OBJECTS_ARRAY.1.nestedArrayInstructions.0.0.instructions',
{
lang: 'en',
},
),
).toBe('Please try again');

expect(
i18nService.translate('test.OBJECTS_ARRAY.0.instructions', {
lang: 'uk',
}),
).toBe('Будь ласка, перейдіть за посиланням');
expect(
i18nService.translate(
'test.OBJECTS_ARRAY.1.nestedArrayInstructions.0.0.instructions',
{
lang: 'uk',
},
),
).toBe('Будь ласка, спробуйте ще раз');
});

it('i18n service should return fallback translation', () => {
expect(i18nService.translate('test.ENGLISH', { lang: 'nl' })).toBe(
'English',
Expand Down
6 changes: 6 additions & 0 deletions tests/i18n/en/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
},
"ENGLISH": "English",
"ARRAY": ["ONE", "TWO", "THREE"],
"OBJECTS_ARRAY": [
{ "instructions": "Please follow the link" },
{
"nestedArrayInstructions": [[{ "instructions": "Please try again" }]]
}
],
"cat": "Cat",
"ONLY_EN_KEY": "this key only exists in en lang",
"cat_name": "Cat: {name}",
Expand Down
8 changes: 8 additions & 0 deletions tests/i18n/uk/test.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"OBJECTS_ARRAY": [
{ "instructions": "Будь ласка, перейдіть за посиланням" },
{
"nestedArrayInstructions": [
[{ "instructions": "Будь ласка, спробуйте ще раз" }]
]
}
],
"day_interval": {
"one": "{count} день",
"few": "{count} дні",
Expand Down