Skip to content

Commit

Permalink
rebase and adapt to vue module changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienpuissant committed Sep 26, 2024
1 parent 14a2024 commit b466c4a
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package tech.jhipster.lite.generator.client.vue.i18n.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.COMMON;
import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.VUE;

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 HOME_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");

Expand All @@ -26,25 +27,28 @@ public JHipsterModule buildModule(JHipsterModuleProperties 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)
.addDependency(packageName("i18next"), COMMON)
.addDependency(packageName("i18next-vue"), VUE)
.addDependency(packageName("i18next-browser-languagedetector"), 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")
.batch(HOME_CONTEXT_SOURCE, to(INDEX + "home/"))
.addFile("HomeTranslations.ts")
.and()
.batch(ASSETS_SOURCE, to(INDEX + "common/locales/"))
.batch(ASSETS_SOURCE, to(INDEX + "home/locales/"))
.addFile("en.ts")
.addFile("fr.ts")
.and()
.batch(TEST_SOURCE, to(INDEX_TEST))
.addFile("setupTests.ts")
.and()
.batch(TEST_SOURCE, to(INDEX_TEST + "webapp/unit/home/infrastructure/primary/"))
.addFile("HomePageVue.spec.ts")
.and()
.and()
.mandatoryReplacements()
.in(path(INDEX + "main.ts"))
Expand All @@ -53,24 +57,13 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.add(lineBeforeText(PROVIDER_NEEDLE
), "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>")
.in(path(INDEX + "/home/infrastructure/primary/HomepageVue.vue"))
.add(lineAfterRegex("Vue 3 \\+ TypeScript \\+ Vite"), properties.indentation().times(2) + "<h2 v-html=\"$t('home.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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import type { Translations } from '@/Translations';
import { en } from './locales/en';
import { fr } from './locales/fr';

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

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

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

i18n.use(LanguageDetector).init({
Expand All @@ -9,7 +9,7 @@ i18n.use(LanguageDetector).init({
interpolation: {
escapeValue: false,
},
resources: toTranslationResources(commonTranslations),
resources: toTranslationResources(homeTranslations),
});

export default i18n;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import HomepageVue from '@/home/infrastructure/primary/HomepageVue.vue';
import { shallowMount, VueWrapper } from '@vue/test-utils';
import { describe, expect, it } from 'vitest';

let wrapper: VueWrapper;

const wrap = () => {
wrapper = shallowMount(HomepageVue);
};

describe('App I18next', () => {
it('should renders with translation', () => {
wrap();

expect(wrapper.text()).toContain('translationEnabled');
});
});
8 changes: 5 additions & 3 deletions src/test/features/client/vue-i18n.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ Feature: Vue i18n
Scenario: Should apply vue i18n module to vue
When I apply modules to default project
| init |
| prettier |
| typescript |
| 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"
And I should have files in "src/main/webapp/app/home/"
| HomeTranslations.ts |
And I should have files in "src/main/webapp/app/home/locales"
| fr.ts |
| en.ts |
And I should have files in "src/test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void shouldBuildI18nModule() {
"""
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { commonTranslations } from './common/CommonTranslations';
import { homeTranslations } from './home/HomeTranslations';
import { toTranslationResources } from './Translations';
i18n.use(LanguageDetector).init({
Expand All @@ -47,7 +47,7 @@ void shouldBuildI18nModule() {
interpolation: {
escapeValue: false,
},
resources: toTranslationResources(commonTranslations),
resources: toTranslationResources(homeTranslations),
});
export default i18n;
Expand Down Expand Up @@ -93,14 +93,14 @@ void shouldBuildI18nModule() {
"""
)
.and()
.hasFile("src/main/webapp/app/common/CommonTranslations.ts")
.hasFile("src/main/webapp/app/home/HomeTranslations.ts")
.containing(
"""
import type { Translations } from '@/Translations';
import { en } from './locales/en';
import { fr } from './locales/fr';
export const commonTranslations: Translations = { fr, en };
export const homeTranslations: Translations = { fr, en };
"""
)
.and()
Expand All @@ -109,8 +109,8 @@ void shouldBuildI18nModule() {
.containing("import I18NextVue from 'i18next-vue';")
.containing("app.use(I18NextVue, { i18next });")
.and()
.hasFile("src/main/webapp/app/common/primary/homepage/Homepage.html")
.containing("<h2 v-html=\"$t('common.translationEnabled')\"></h2>")
.hasFile("src/main/webapp/app/home/infrastructure/primary/HomepageVue.vue")
.containing("<h2 v-html=\"$t('home.translationEnabled')\"></h2>")
.and()
.hasFile("src/test/setupTests.ts")
.containing(
Expand All @@ -126,33 +126,33 @@ void shouldBuildI18nModule() {
.hasFile("vitest.config.ts")
.containing("setupFiles: ['./src/test/setupTests.ts']")
.and()
.hasFile("src/main/webapp/app/common/locales/en.ts")
.hasFile("src/main/webapp/app/home/locales/en.ts")
.containing(
"""
import type { Translation } from '@/Translations';
export const en: Translation = {
common: {
home: {
translationEnabled: 'Internationalization enabled',
},
};
"""
)
.and()
.hasFile("src/main/webapp/app/common/locales/fr.ts")
.hasFile("src/main/webapp/app/home/locales/fr.ts")
.containing(
"""
import type { Translation } from '@/Translations';
export const fr: Translation = {
common: {
home: {
translationEnabled: 'Internationalisation activée',
},
};
"""
)
.and()
.hasFile("src/test/webapp/unit/common/primary/homepage/Homepage.spec.ts")
.hasFile("src/test/webapp/unit/home/infrastructure/primary/HomepageVue.spec.ts")
.containing("describe('App I18next', () => {");
}

Expand All @@ -161,13 +161,16 @@ private ModuleFile mainFile() {
}

private ModuleFile homepage() {
return file("src/test/resources/projects/vue/Homepage.html.template", "src/main/webapp/app/common/primary/homepage/Homepage.html");
return file(
"src/test/resources/projects/vue/HomepageVue.vue.template",
"src/main/webapp/app/home/infrastructure/primary/HomepageVue.vue"
);
}

private ModuleFile homepageTest() {
return file(
"src/test/resources/projects/vue/Homepage.spec.ts.template",
"src/test/webapp/unit/common/primary/homepage/Homepage.spec.ts"
"src/test/resources/projects/vue/HomepageVue.spec.ts.template",
"src/test/webapp/unit/home/infrastructure/primary/HomepageVue.spec.ts"
);
}

Expand Down
23 changes: 0 additions & 23 deletions src/test/resources/projects/vue/Homepage.html.template

This file was deleted.

71 changes: 71 additions & 0 deletions src/test/resources/projects/vue/HomepageVue.vue.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<template>
<div id="app">
<img
alt="Vue logo"
src="../../../../content/images/VueLogo.png"
/>
<br />
<img
alt="JHipster logo"
src="../../../../content/images/JHipster-Lite-neon-green.png"
/>
<h1>{{ appName }}: Vue 3 + TypeScript + Vite</h1>

<p>
<a
href="https://vitejs.dev/guide/features.html"
target="_blank"
rel="noopener"
> Vite Documentation </a>
|
<a
href="https://v3.vuejs.org/"
target="_blank"
rel="noopener"
>Vue 3 Documentation</a>
</p>

<p>
Edit
<code>src/main/webapp/app/home/infrastructure/primary/HomepageVue.vue</code> to test hot module replacement.
</p>
</div>
</template>

<script lang="ts">
export default {
name: 'HomepageVue',
data: () => {
return {
appName: 'Dummy',
};
},
};
</script>

<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}

a {
color: #42b983;
}

label {
margin: 0 0.5em;
font-weight: bold;
}

code {
background-color: #eee;
padding: 2px 4px;
border-radius: 4px;
color: #304455;
}
</style>

0 comments on commit b466c4a

Please sign in to comment.