Skip to content

Commit

Permalink
not ignoring Translation.ts but unit test and bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienpuissant committed Sep 30, 2024
1 parent 14b3cdf commit 2f7614a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.and()
.in(path("./vitest.config.ts"))
.add(lineAfterRegex("test:"), properties.indentation().times(2) + "setupFiles: ['./src/test/setupTests.ts'],")
.add(lineAfterText("'src/main/webapp/app/main.ts',"), properties.indentation().times(4) + "'src/main/webapp/app/Translations.ts',")
.and()
.and()
.build();
Expand Down
13 changes: 11 additions & 2 deletions src/main/resources/generator/client/common/i18n/Translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const toLanguage = ([key, value]: [string, ResourceKey]): [string, ResourceLangu
},
];

const mergeTranslations = (translations: Translations[]): Translations =>
export const mergeTranslations = (translations: Translations[]): Translations =>
translations
.flatMap(translations => Object.entries(translations))
.reduce(
(acc, [key, translation]) => ({
...acc,
[key]: acc[key] ? { ...acc[key], ...translation } : translation,
[key]: acc[key] ? { ...acc[key], ...deepMerge(acc[key], translation) } : translation,
}),
{} as Translations,
);
Expand All @@ -31,3 +31,12 @@ export const toTranslationResources = (...translations: Translations[]): Resourc
}),
{},
);

const deepMerge = (target: Translation, source: Translation): Translation => {
for (const key of Object.keys(source)) {
if (source[key] instanceof Object && key in target) {
Object.assign(source[key], deepMerge(target[key] as Translation, source[key] as Translation));
}
}
return { ...target, ...source };
};
34 changes: 34 additions & 0 deletions src/main/resources/generator/client/common/i18n/i18n.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import i18n from '@/i18n';
import { mergeTranslations } from '@/Translations';

describe('i18n configuration', () => {
it('loads en translation', () => {
Expand All @@ -8,4 +9,37 @@ describe('i18n configuration', () => {
it('loads fr translation', () => {
expect(i18n.getResourceBundle('fr', '')['home']['translationEnabled']).toEqual('Internationalisation activée');
});

describe('mergeTranslations function', () => {
it('merges translations correctly when keys overlap', () => {
const translationSet1 = {
en: {
home: {
translationEnabled: 'Internationalization enabled',
welcome: 'Welcome',
},
},
};

const translationSet2 = {
en: {
home: {
anotherMessage: 'Another message',
},
},
};

const mergedResult = mergeTranslations([translationSet1, translationSet2]);

expect(mergedResult).toEqual({
en: {
home: {
translationEnabled: 'Internationalization enabled',
welcome: 'Welcome',
anotherMessage: 'Another message',
},
},
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-http-backend';

i18n
void i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ void shouldBuildI18nModule() {
.and()
.hasFile("src/main/webapp/app/home/locales/fr.ts")
.and()
.hasFile("src/test/webapp/unit/home/infrastructure/primary/HomepageVue.spec.ts")
.and()
.hasFile("vitest.config.ts")
.containing("src/main/webapp/app/Translations.ts");
.hasFile("src/test/webapp/unit/home/infrastructure/primary/HomepageVue.spec.ts");
}

private ModuleFile mainFile() {
Expand Down

0 comments on commit 2f7614a

Please sign in to comment.