Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit f6812ad

Browse files
authored
Making i18n specs more maintainable. Covering additional use case. (#366)
* chore: refactor i18n to demonstrate i18n loader vs json-only loader. Generate tests and source
2 parents abb95e0 + 35a6a36 commit f6812ad

File tree

7 files changed

+78
-26
lines changed

7 files changed

+78
-26
lines changed

cypress/component/advanced/i18n/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# i18n example
22

3-
Using [vue-i18n](https://kazupon.github.io/vue-i18n/) and [@intlify/vue-i18n-loader](https://github.com/intlify/vue-i18n-loader/tree/master) stable.
4-
5-
See [TranslatedMessage.vue](TranslatedMessage.vue) and its test in [spec.js](spec.js)
3+
* For JSON-only, see [TranslatedJSONMessage.vue](TranslatedJSONMessage.vue) and its test in [spec.js](spec.js)
4+
* From i18n support loading, see [TranslatedI18nMessage.vue](TranslatedI18nMessage.vue) and its test in [spec.js](spec.js)
65

76
![i18n test in action](./images/i18n.gif)
87

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- uses special loader to load i18n blocks -->
2+
<!-- see https://github.com/intlify/vue-i18n-loader -->
3+
<!-- You can also inline the translations like <i18n>{...}</i18n> -->
4+
<i18n src="./translations.json"/>
5+
6+
<template>
7+
<div id="app">
8+
<label for="locale">locale</label>
9+
<select v-model="$i18n.locale" id="locale">
10+
<option v-for="locale in $i18n.availableLocales" :key="locale">
11+
{{ locale }}
12+
</option>
13+
</select>
14+
<p>message: {{ $t('hello') }}</p>
15+
</div>
16+
</template>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<template>
2+
<div id="app">
3+
<label for="locale">locale</label>
4+
<select v-model="$i18n.locale" id="locale">
5+
<option v-for="locale in $i18n.availableLocales" :key="locale">
6+
{{ locale }}
7+
</option>
8+
</select>
9+
<p>message: {{ $t('hello') }}</p>
10+
</div>
11+
</template>
Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
11
/// <reference types="cypress" />
22
import Vue from 'vue'
3-
import TranslatedMessage from './TranslatedMessage.vue'
3+
import TranslatedMessageWithJSON from './TranslatedJSONMessage.vue'
4+
import TranslatedMessageI18nBlock from './TranslatedI18nMessage.vue'
45
import VueI18n from 'vue-i18n'
5-
import { mountCallback } from 'cypress-vue-unit-test'
6+
import { mount } from 'cypress-vue-unit-test'
7+
import messages from './translations.json'
68

7-
Vue.use(VueI18n)
8-
const i18n = new VueI18n({ locale: 'en' })
9+
function expectHelloWorldGreeting() {
10+
cy.viewport(400, 200)
11+
const allLocales = Cypress.vue.$i18n.availableLocales
912

10-
describe('VueI18n', () => {
11-
beforeEach(mountCallback(TranslatedMessage, { i18n }))
12-
13-
it('shows English, Japanese and Russian greeting', () => {
14-
cy.viewport(400, 200)
13+
// ensure we don't strip locales
14+
expect(Object.keys(messages)).to.have.members(allLocales)
1515

16-
cy.get('select').select('en').should('have.value', 'en')
17-
cy.contains('message: hello')
16+
allLocales.forEach((locale) => {
17+
cy.get('select').select(locale).should('have.value', locale)
18+
const hello = messages[locale].hello
19+
cy.contains(hello)
20+
})
21+
}
1822

19-
cy.get('select').select('fa').should('have.value', 'fa')
20-
cy.contains('message: سلام دنیا')
23+
describe('VueI18n', () => {
24+
Vue.use(VueI18n)
2125

22-
cy.get('select').select('ja').should('have.value', 'ja')
23-
cy.contains('message: こんにちは、世界')
26+
describe('with i18n block', () => {
27+
beforeEach(() => {
28+
const i18n = new VueI18n({ locale: 'en' })
29+
mount(TranslatedMessageI18nBlock, { i18n })
30+
})
2431

25-
cy.get('select').select('ru').should('have.value', 'ru')
26-
cy.contains('message: Привет мир')
32+
it('shows HelloWorld for all locales', expectHelloWorldGreeting)
2733
})
2834

29-
// TODO how to load messages not from i18n block but from external JSON file?
30-
// then we could reuse the messages to check the contents
35+
describe('with messages argument', () => {
36+
beforeEach(() => {
37+
const i18n = new VueI18n({ locale: 'en', messages })
38+
mount(TranslatedMessageWithJSON, { i18n })
39+
})
40+
41+
it('shows HelloWorld for all locales', expectHelloWorldGreeting)
42+
})
3143
})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"en": {
3+
"hello": "hello world!"
4+
},
5+
"fa": {
6+
"hello": "سلام دنیا"
7+
},
8+
"ja": {
9+
"hello": "こんにちは、世界"
10+
},
11+
"ru": {
12+
"hello": "Привет мир"
13+
}
14+
}

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"tailwindcss": "1.1.4",
7272
"typescript": "3.9.6",
7373
"vue": "2.6.11",
74-
"vue-i18n": "7.8.1",
74+
"vue-i18n": "8.9.0",
7575
"vue-loader": "15.9.3",
7676
"vue-router": "3.0.5",
7777
"vue-style-loader": "4.1.2",

0 commit comments

Comments
 (0)