-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create vue internalization module using i18next 23.14.0 with language detector see https://i18next.github.io/i18next-vue/introduction.html
- Loading branch information
1 parent
bae298f
commit b236543
Showing
17 changed files
with
401 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...a/tech/jhipster/lite/generator/client/vue/i18n/application/VueI18nApplicationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/tech/jhipster/lite/generator/client/vue/i18n/domain/VueI18nModuleFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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/vue/i18n/src/main/webapp/app"); | ||
private static final JHipsterSource ASSETS_FR_SOURCE = from("client/common/i18n/locales/fr"); | ||
private static final JHipsterSource ASSETS_EN_SOURCE = from("client/common/i18n/locales/en"); | ||
private static final JHipsterSource TEST_SOURCE = from("client/vue/i18n/src/test"); | ||
|
||
private static final String INDEX = "src/main/webapp/"; | ||
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) | ||
.addDependency(packageName("i18next-http-backend"), VersionSource.COMMON) | ||
.and() | ||
.files() | ||
.batch(APP_SOURCE, to(INDEX + "/app")) | ||
.addFile("i18n.ts") | ||
.and() | ||
.batch(ASSETS_EN_SOURCE, to(INDEX + "content/locales/en/")) | ||
.addFile("translation.json") | ||
.and() | ||
.batch(ASSETS_FR_SOURCE, to(INDEX + "content/locales/fr/")) | ||
.addFile("translation.json") | ||
.and() | ||
.batch(TEST_SOURCE, to(INDEX_TEST)) | ||
.addFile("setupTests.ts") | ||
.and() | ||
.and() | ||
.mandatoryReplacements() | ||
.in(path(INDEX + "app/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 + "app/common/primary/homepage/Homepage.html")) | ||
.add(lineAfterRegex("Vue 3 \\+ TypeScript \\+ Vite"), properties.indentation().times(1) + "<p v-html=\"$t('translationEnabled')\"></p>") | ||
.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 | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ter/lite/generator/client/vue/i18n/infrastructure/primary/VueI18nModuleConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/tech/jhipster/lite/generator/client/vue/i18n/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions
22
src/main/resources/generator/client/vue/i18n/src/main/webapp/app/i18n.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import i18n from 'i18next'; | ||
|
||
import Backend from 'i18next-http-backend'; | ||
import LanguageDetector from 'i18next-browser-languagedetector'; | ||
|
||
i18n | ||
.use(Backend) | ||
.use(LanguageDetector) | ||
|
||
.init({ | ||
fallbackLng: 'en', | ||
debug: false, | ||
|
||
interpolation: { | ||
escapeValue: false, | ||
}, | ||
backend: { | ||
loadPath: '../content/locales/{{ lng }}/{{ ns }}.json', | ||
}, | ||
}); | ||
|
||
export default i18n; |
5 changes: 5 additions & 0 deletions
5
src/main/resources/generator/client/vue/i18n/src/test/setupTests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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 | | ||
And I should have files in "src/main/webapp/content/locales/en" | ||
| translation.json | | ||
And I should have files in "src/main/webapp/content/locales/fr" | ||
| translation.json | | ||
And I should have files in "src/test" | ||
| setupTests.ts | |
127 changes: 127 additions & 0 deletions
127
...st/java/tech/jhipster/lite/generator/client/vue/i18n/domain/VueI18nModuleFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package tech.jhipster.lite.generator.client.vue.i18n.domain; | ||
|
||
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import tech.jhipster.lite.TestFileUtils; | ||
import tech.jhipster.lite.UnitTest; | ||
import tech.jhipster.lite.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.module.domain.JHipsterModulesFixture; | ||
|
||
@UnitTest | ||
class VueI18nModuleFactoryTest { | ||
|
||
private static final VueI18nModuleFactory factory = new VueI18nModuleFactory(); | ||
|
||
@Test | ||
void shouldBuildI18nModule() { | ||
JHipsterModule module = factory.buildModule( | ||
JHipsterModulesFixture.propertiesBuilder(TestFileUtils.tmpDirForTest()).projectBaseName("jhipster").build() | ||
); | ||
|
||
JHipsterModuleAsserter asserter = assertThatModuleWithFiles( | ||
module, | ||
packageJsonFile(), | ||
mainFile(), | ||
homepage(), | ||
homepageTest(), | ||
vitest() | ||
); | ||
asserter | ||
.hasFile("package.json") | ||
.containing(nodeDependency("i18next")) | ||
.containing(nodeDependency("i18next-vue")) | ||
.containing(nodeDependency("i18next-browser-languagedetector")) | ||
.containing(nodeDependency("i18next-http-backend")) | ||
.and() | ||
.hasFile("src/main/webapp/app/i18n.ts") | ||
.containing( | ||
""" | ||
import i18n from 'i18next'; | ||
import Backend from 'i18next-http-backend'; | ||
import LanguageDetector from 'i18next-browser-languagedetector'; | ||
i18n | ||
.use(Backend) | ||
.use(LanguageDetector) | ||
.init({ | ||
fallbackLng: 'en', | ||
debug: false, | ||
interpolation: { | ||
escapeValue: false, | ||
}, | ||
backend: { | ||
loadPath: '../content/locales/{{ lng }}/{{ ns }}.json', | ||
}, | ||
}); | ||
export default i18n; | ||
""" | ||
) | ||
.and() | ||
.hasFile("src/main/webapp/app/main.ts") | ||
.containing("import i18next from './i18n';") | ||
.containing("import I18NextVue from 'i18next-vue';") | ||
.containing("app.use(I18NextVue, { i18next });") | ||
.and() | ||
.hasFile("src/main/webapp/app/common/primary/homepage/Homepage.html") | ||
.containing("<p v-html=\"$t('translationEnabled')\"></p>") | ||
.and() | ||
.hasFile("src/test/setupTests.ts") | ||
.containing( | ||
""" | ||
import { config } from '@vue/test-utils'; | ||
config.global.mocks = { | ||
$t: (msg) => msg, | ||
}; | ||
""" | ||
) | ||
.and() | ||
.hasFile("vitest.config.ts") | ||
.containing("setupFiles: ['./src/test/setupTests.ts']") | ||
.and() | ||
.hasFile("src/main/webapp/content/locales/en/translation.json") | ||
.containing( | ||
""" | ||
{ | ||
"translationEnabled": "Internationalization enabled" | ||
} | ||
""" | ||
) | ||
.and() | ||
.hasFile("src/main/webapp/content/locales/fr/translation.json") | ||
.containing( | ||
""" | ||
{ | ||
"translationEnabled": "Internationalisation activée" | ||
} | ||
""" | ||
) | ||
.and() | ||
.hasFile("src/test/webapp/unit/common/primary/homepage/Homepage.spec.ts") | ||
.containing("describe('App I18next', () => {"); | ||
} | ||
|
||
private ModuleFile mainFile() { | ||
return file("src/test/resources/projects/vue/main.ts.template", "src/main/webapp/app/main.ts"); | ||
} | ||
|
||
private ModuleFile homepage() { | ||
return file("src/test/resources/projects/vue/Homepage.html.template", "src/main/webapp/app/common/primary/homepage/Homepage.html"); | ||
} | ||
|
||
private ModuleFile homepageTest() { | ||
return file( | ||
"src/test/resources/projects/vue/Homepage.spec.ts.template", | ||
"src/test/webapp/unit/common/primary/homepage/Homepage.spec.ts" | ||
); | ||
} | ||
|
||
private ModuleFile vitest() { | ||
return file("src/test/resources/projects/vue/vitest.config.ts.template", "./vitest.config.ts"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<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> | ||
Recommended IDE setup: | ||
<a href="https://code.visualstudio.com/" target="_blank" rel="noopener">VSCode</a> | ||
+ | ||
<a href="https://github.com/johnsoncodehk/volar" target="_blank" rel="noopener">Volar</a> | ||
</p> | ||
|
||
<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/common/primary/app/AppVue.vue</code> to test hot module replacement. | ||
</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { shallowMount, VueWrapper } from '@vue/test-utils'; | ||
import { HomepageVue } from '@/common/primary/homepage'; | ||
|
||
let wrapper: VueWrapper; | ||
|
||
const wrap = () => { | ||
wrapper = shallowMount(HomepageVue); | ||
}; | ||
|
||
describe('App', () => { | ||
it('should exist', () => { | ||
wrap(); | ||
|
||
expect(wrapper.exists()).toBeTruthy(); | ||
}); | ||
}); |
Oops, something went wrong.