Skip to content

Commit

Permalink
add vue internationalization
Browse files Browse the repository at this point in the history
create vue internalization module using i18next 23.14.0 with language detector
see https://i18next.github.io/i18next-vue/introduction.html
  • Loading branch information
fabienpuissant committed Sep 4, 2024
1 parent 6f77e5a commit 2d214da
Show file tree
Hide file tree
Showing 18 changed files with 497 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class ReactI18nModuleConfiguration {

@Bean
JHipsterModuleResource i18nModule(ReactI18nApplicationService i18n) {
JHipsterModuleResource reactI18nModule(ReactI18nApplicationService i18n) {
return JHipsterModuleResource.builder()
.slug(REACT_I18N)
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().build())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tech.jhipster.lite.generator.client.vue.i18n.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.client.vue.i18n.domain.VueI18nModuleFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Service
public class VueI18nApplicationService {

private final VueI18nModuleFactory factory;

public VueI18nApplicationService() {
factory = new VueI18nModuleFactory();
}

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
return factory.buildModule(properties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package tech.jhipster.lite.generator.client.vue.i18n.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.packagejson.VersionSource;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.shared.error.domain.Assert;

public class VueI18nModuleFactory {

private static final JHipsterSource APP_SOURCE = from("client/common/i18n");
private static final JHipsterSource COMMON_CONTEXT_SOURCE = from("client/common/i18n/app");
private static final JHipsterSource ASSETS_SOURCE = from("client/common/i18n/app/locales");
private static final JHipsterSource TEST_SOURCE = from("client/vue/i18n/src/test");

private static final String INDEX = "src/main/webapp/app/";
private static final String INDEX_TEST = "src/test/";

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);

//@formatter:off
return moduleBuilder(properties)
.packageJson()
.addDependency(packageName("i18next"), VersionSource.COMMON)
.addDependency(packageName("i18next-vue"), VersionSource.VUE)
.addDependency(packageName("i18next-browser-languagedetector"), VersionSource.COMMON)
.and()
.files()
.batch(APP_SOURCE, to(INDEX))
.addFile("i18n.ts")
.addFile("Translations.ts")
.and()
.batch(COMMON_CONTEXT_SOURCE, to(INDEX + "common/"))
.addFile("CommonTranslations.ts")
.and()
.batch(ASSETS_SOURCE, to(INDEX + "common/locales/"))
.addFile("en.ts")
.addFile("fr.ts")
.and()
.batch(TEST_SOURCE, to(INDEX_TEST))
.addFile("setupTests.ts")
.and()
.and()
.mandatoryReplacements()
.in(path(INDEX + "main.ts"))
.add(lineBeforeText("// jhipster-needle-main-ts-import"), "import i18next from './i18n';")
.add(lineBeforeText("// jhipster-needle-main-ts-import"), "import I18NextVue from 'i18next-vue';")
.add(lineBeforeText("app.mount('#app');"), "app.use(I18NextVue, { i18next });")
.and()
.in(path(INDEX + "/common/primary/homepage/Homepage.html"))
.add(lineAfterRegex("Vue 3 \\+ TypeScript \\+ Vite"), properties.indentation().times(1) + "<h2 v-html=\"$t('common.translationEnabled')\"></h2>")
.and()
.in(path("./vitest.config.ts"))
.add(lineAfterRegex("test:"), properties.indentation().times(2) + "setupFiles: ['./src/test/setupTests.ts'],")
.and()
.in(path(INDEX_TEST + "webapp/unit/common/primary/homepage/Homepage.spec.ts"))
.add(append(), LINE_BREAK + """
describe('App I18next', () => {
it('should renders with translation', () => {
wrap();
expect(wrapper.text()).toContain("translationEnabled");
});
});
""")
.and()
.and()
.build();
//@formatter:off
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tech.jhipster.lite.generator.client.vue.i18n.infrastructure.primary;

import static tech.jhipster.lite.generator.slug.domain.JHLiteFeatureSlug.CLIENT_INTERNATIONALIZATION;
import static tech.jhipster.lite.generator.slug.domain.JHLiteModuleSlug.VUE_CORE;
import static tech.jhipster.lite.generator.slug.domain.JHLiteModuleSlug.VUE_I18N;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.client.vue.i18n.application.VueI18nApplicationService;
import tech.jhipster.lite.module.domain.resource.JHipsterModuleOrganization;
import tech.jhipster.lite.module.domain.resource.JHipsterModulePropertiesDefinition;
import tech.jhipster.lite.module.domain.resource.JHipsterModuleResource;

@Configuration
class VueI18nModuleConfiguration {

@Bean
JHipsterModuleResource vueI18nModule(VueI18nApplicationService i18n) {
return JHipsterModuleResource.builder()
.slug(VUE_I18N)
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().build())
.apiDoc("Frontend - Vue", "Add vue internationalization")
.organization(JHipsterModuleOrganization.builder().feature(CLIENT_INTERNATIONALIZATION).addDependency(VUE_CORE).build())
.tags("client", "vue", "i18n")
.factory(i18n::buildModule);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tech.jhipster.lite.BusinessContext
package tech.jhipster.lite.generator.client.vue.i18n;
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public enum JHLiteModuleSlug implements JHipsterModuleSlugFactory {
SVELTE_CORE("svelte-core"),
TYPESCRIPT("typescript"),
VUE_CORE("vue-core"),
VUE_I18N("vue-i18next"),
VUE_PINIA("vue-pinia"),
TS_PAGINATION_DOMAIN("ts-pagination-domain"),
TS_REST_PAGINATION("ts-rest-pagination");
Expand Down
33 changes: 33 additions & 0 deletions src/main/resources/generator/client/common/i18n/Translations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { Resource, ResourceKey, ResourceLanguage } from 'i18next';

export type Translation = Record<string, unknown>;
export type Translations = Record<string, Translation>;

const toLanguage = ([key, value]: [string, ResourceKey]): [string, ResourceLanguage] => [
key,
{
translation: value,
},
];

const mergeTranslations = (translations: Array<Translations>): Translations =>
translations
.flatMap(translations => Object.entries(translations))
.reduce(
(acc, [key, translation]) => ({
...acc,
[key]: acc[key] ? { ...acc[key], ...translation } : translation,
}),
{} as Translations,
);

export const toTranslationResources = (...translations: Array<Translations>): Resource =>
Object.entries(mergeTranslations(translations))
.map(toLanguage)
.reduce(
(acc, current) => ({
...acc,
[current[0]]: current[1],
}),
{},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Translations } from '@/Translations';
import { en } from './locales/en';
import { fr } from './locales/fr';

export const commonTranslations: Translations = { fr, en };
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Translation } from '@/Translations';

export const en: Translation = {
common: {
translationEnabled: 'Internationalization enabled',
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Translation } from '@/Translations';

export const fr: Translation = {
common: {
translationEnabled: 'Internationalisation activée',
},
};
15 changes: 15 additions & 0 deletions src/main/resources/generator/client/common/i18n/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { commonTranslations } from './common/CommonTranslations';
import { toTranslationResources } from './Translations';

i18n.use(LanguageDetector).init({
fallbackLng: 'en',
debug: false,
interpolation: {
escapeValue: false,
},
resources: toTranslationResources(commonTranslations),
});

export default i18n;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { config } from '@vue/test-utils';

config.global.mocks = {
$t: msg => msg,
};
1 change: 1 addition & 0 deletions src/main/resources/generator/dependencies/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "Apache-2.0",
"dependencies": {
"axios": "1.7.5",
"i18next-vue": "5.0.0",
"pinia": "2.2.2",
"pinia-plugin-persistedstate": "3.2.3",
"vue": "3.4.38",
Expand Down
17 changes: 17 additions & 0 deletions src/test/features/client/vue-i18n.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: Vue i18n

Scenario: Should apply vue i18n module to vue
When I apply modules to default project
| init |
| vue-core |
| vue-i18next |
Then I should have files in "src/main/webapp/app"
| i18n.ts |
| Translations.ts |
And I should have files in "src/main/webapp/app/common/"
| CommonTranslations.ts |
And I should have files in "src/main/webapp/app/common/locales"
| fr.ts |
| en.ts |
And I should have files in "src/test"
| setupTests.ts |
Loading

0 comments on commit 2d214da

Please sign in to comment.