Skip to content

Commit

Permalink
fix: i18n translations
Browse files Browse the repository at this point in the history
  • Loading branch information
feenko committed Dec 2, 2024
1 parent 096b1d0 commit 2fd23c9
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/lib/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,23 @@ export class I18n {
try {
const files = await this.getJsonFiles(localePath);

const translations = await Promise.all(
files.map(async file => {
const content = await fs.readFile(file, 'utf8');
return {
content,
namespace: path
.relative(localePath, file)
.replace(/\.[^/.]+$/, '')
.split(path.sep),
};
})
);
for (const file of files) {
const content = JSON.parse(await fs.readFile(file, 'utf8'));
const namespace = path
.relative(localePath, file)
.replace(/\.[^/.]+$/, '')
.split(path.sep);

for (const { content, namespace } of translations) {
let current = mergedTranslations;

for (let i = 0; i < namespace.length - 1; i++) {
for (let i = 0; i < namespace.length; i++) {
const segment = namespace[i];
current[segment] = current[segment] || {};
current = current[segment] as Record<string, unknown>;
if (i === namespace.length - 1) {
current[segment] = { ...(current[segment] as object || {}), ...content };
} else {
current[segment] = current[segment] || {};
current = current[segment] as Record<string, unknown>;
}
}

const lastSegment = namespace[namespace.length - 1];
current[lastSegment] = { ...((current[lastSegment] as object) || {}), ...content };
}
} catch (error) {
logger.error(`Failed to load files from '${localePath}':`, error);
Expand Down

0 comments on commit 2fd23c9

Please sign in to comment.