Skip to content

Commit 441b9a5

Browse files
authored
Added Percy snapshot for ja translations (#73)
* Improved separation of concerns for <LocaleMenu> * Standardized translations for <LocaleMenu> * Added Percy snapshot for ja translations * Added tests for <LocaleMenu> Co-authored-by: ijlee2 <ijlee2@users.noreply.github.com>
1 parent d39c5d0 commit 441b9a5

File tree

13 files changed

+207
-45
lines changed

13 files changed

+207
-45
lines changed
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import Component from '@glimmer/component';
2-
import { inject as service } from '@ember/service';
31
import { action } from '@ember/object';
4-
2+
import { inject as service } from '@ember/service';
3+
import Component from '@glimmer/component';
54

65
export default class LocaleMenuComponent extends Component {
7-
@service intl;
6+
@service locale;
87

9-
supportedLocales = ['en-US', 'fr-FR', 'pt-BR', 'es', 'ja'];
8+
@action updateLocale(event) {
9+
const locale = event.target.value;
1010

11-
@action
12-
updateLocale(evt) {
13-
if (evt.target.value) {
14-
this.intl.setLocale(evt.target.value);
15-
}
11+
this.locale.updateSiteLocale(locale);
1612
}
1713
}
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
<select
2-
data-test-field="Locale"
3-
{{on "input" this.updateLocale}}
4-
>
5-
<option value="">
6-
{{t "component.locale-menu.select-your-preferred-language"}}
7-
</option>
1+
<select
2+
class="locale-menu"
3+
data-test-field="Locale"
4+
{{on "input" this.updateLocale}}
5+
>
6+
<option value="" disabled selected>
7+
{{t "component.locale-menu.select-your-preferred-language"}}
8+
</option>
89

9-
{{#each this.supportedLocales as |locale|}}
10-
<option value={{locale}}>
11-
{{t (concat "component.locale-menu.locale-" locale)}}
12-
</option>
13-
{{/each}}
10+
{{#each this.locale.menuOptions as |menuOption|}}
11+
<option data-test-option value={{menuOption.value}}>
12+
{{menuOption.label}}
13+
</option>
14+
{{/each}}
1415
</select>

app/services/locale.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Service, { inject as service } from '@ember/service';
2+
3+
/*
4+
To help with maintainenance, please list
5+
the supported locales in alphabetical order.
6+
*/
7+
const supportedLocales = new Set([
8+
'en-US',
9+
'es',
10+
'fr-FR',
11+
'ja',
12+
'pt-BR',
13+
]);
14+
15+
export default class LocaleService extends Service {
16+
@service intl;
17+
18+
menuOptions = Array.from(supportedLocales)
19+
.map(locale => {
20+
return {
21+
label: this.intl.t(`component.locale-menu.locale-${locale}`).toString(),
22+
value: locale,
23+
};
24+
})
25+
.sort((a, b) => {
26+
const aValue = a.label.toLowerCase();
27+
const bValue = b.label.toLowerCase();
28+
29+
if (aValue > bValue) return 1;
30+
if (aValue < bValue) return -1;
31+
return 0;
32+
});
33+
34+
updateSiteLocale(locale) {
35+
if (!supportedLocales.has(locale)) {
36+
return;
37+
}
38+
39+
this.intl.setLocale(locale);
40+
}
41+
}

app/styles/app.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ h2 .permalink {
7070
color: #c1c1c1;
7171
padding: 0 15px 0 5px;
7272
}
73+
74+
.locale-menu {
75+
padding: 0.25em 0.5em;
76+
}
77+
7378
.intro {
7479
font-size: 1.1em;
7580
line-height: 1.5;

tests/acceptance/homepage/en-us-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ module('Acceptance | Homepage | en-US', function(hooks) {
2424
.hasAttribute('lang', 'en-us', 'We set the correct lang attribute.');
2525
});
2626

27+
2728
test('We can change the site language', async function(assert) {
2829
await visit('/');
30+
2931
assert.dom('#generating-files')
3032
.hasText('§ Generating Files', 'We see the site in English.');
3133

3234
await fillIn('[data-test-field="Locale"]', 'pt-BR');
33-
assert.dom('#generating-files')
34-
.hasText('§ Geração de arquivos', 'We see the site in Portugese (Brazil).');
3535

36-
await fillIn('[data-test-field="Locale"]', '');
3736
assert.dom('#generating-files')
38-
.hasText('§ Geração de arquivos', 'We still see the site in Portugese (Brazil).');
37+
.hasText('§ Geração de arquivos', 'We see the site in Portugese (Brazil).');
3938

4039
await fillIn('[data-test-field="Locale"]', 'fr-FR');
40+
4141
assert.dom('#generating-files')
4242
.hasText('§ Génération de fichiers', 'We see the site in French.');
4343
});

tests/acceptance/homepage/ja-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { visit } from '@ember/test-helpers';
2+
import percySnapshot from '@percy/ember';
3+
import { setupIntl } from 'ember-intl/test-support';
4+
import { setupApplicationTest } from 'ember-qunit';
5+
import { module, test } from 'qunit';
6+
7+
module('Acceptance | Homepage | ja', function(hooks) {
8+
setupApplicationTest(hooks);
9+
setupIntl(hooks, 'ja');
10+
11+
12+
test('Percy snapshot', async function(assert) {
13+
await visit('/');
14+
await percySnapshot(assert);
15+
16+
assert.ok(true);
17+
});
18+
19+
20+
test('We set the correct lang attribute in <html> element', async function(assert) {
21+
await visit('/');
22+
23+
assert.dom(document.querySelector('html'))
24+
.hasAttribute('lang', 'ja', 'We set the correct lang attribute.');
25+
});
26+
});
Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,46 @@
1-
import { module, test } from 'qunit';
2-
import { setupRenderingTest } from 'ember-qunit';
3-
import { render } from '@ember/test-helpers';
1+
import { fillIn, render } from '@ember/test-helpers';
42
import { hbs } from 'ember-cli-htmlbars';
3+
import { setupIntl } from 'ember-intl/test-support';
4+
import { setupRenderingTest } from 'ember-qunit';
5+
import { module, test } from 'qunit';
56

67
module('Integration | Component | locale-menu', function(hooks) {
78
setupRenderingTest(hooks);
9+
setupIntl(hooks);
10+
11+
12+
test('renders options for supported locales', async function(assert) {
13+
await render(hbs`
14+
<LocaleMenu />
15+
`);
16+
17+
assert.dom('[data-test-field="Locale"]')
18+
.hasValue('', 'The default option is selected.')
19+
20+
assert.dom('[data-test-option]')
21+
.exists({ count: 5 }, 'There are 5 non-default options.');
22+
});
23+
24+
25+
test('allows the user to update the site locale', async function(assert) {
26+
await render(hbs`
27+
<LocaleMenu />
28+
`);
29+
30+
await fillIn('[data-test-field="Locale"]', 'pt-BR');
31+
32+
assert.strictEqual(
33+
this.intl.primaryLocale,
34+
'pt-br',
35+
'The site locale is pt-BR.'
36+
);
837

9-
test('it renders', async function(assert) {
10-
// Set any properties with this.set('myProperty', 'value');
11-
// Handle any actions with this.set('myAction', function(val) { ... });
38+
await fillIn('[data-test-field="Locale"]', 'fr-FR');
1239

13-
await render(hbs`<LocaleMenu />`);
14-
assert.dom('select option').exists({ count: 6 });
40+
assert.strictEqual(
41+
this.intl.primaryLocale,
42+
'fr-fr',
43+
'The site locale is fr-FR.'
44+
);
1545
});
1646
});

tests/unit/services/locale-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { setupTest } from 'ember-qunit';
2+
import { module, test } from 'qunit';
3+
4+
module('Unit | Service | locale', function(hooks) {
5+
setupTest(hooks);
6+
7+
test('menuOptions includes user-friendly labels', function(assert) {
8+
const locale = this.owner.lookup('service:locale');
9+
10+
assert.deepEqual(
11+
locale.menuOptions.map(({ label }) => label),
12+
[
13+
'English (US)',
14+
'Français',
15+
'Português (do Brasil)',
16+
'Spanish',
17+
'日本語',
18+
]
19+
);
20+
});
21+
});

translations/en-us.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ component:
1818
edit-translation-text: Edit Translation
1919
locale-menu:
2020
select-your-preferred-language: Select your preferred language
21+
#
22+
# For convenience, the translations of locale menu options
23+
# will be the same across all locales.
24+
#
25+
# To help with maintenance, please list keys in alphabetical
26+
# order.
27+
#
2128
locale-en-US: English (US)
22-
locale-fr-FR: Français
23-
locale-pt-BR: Português (do Brasil)
2429
locale-es: Spanish
30+
locale-fr-FR: Français
2531
locale-ja: 日本語
32+
locale-pt-BR: Português (do Brasil)

translations/es.yaml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ component:
1818
edit-translation-text: Editar traducción
1919
locale-menu:
2020
select-your-preferred-language: Seleccione su idioma
21-
locale-en-US: Ingles (US)
22-
locale-fr-FR: Francés
23-
locale-pt-BR: Portugués (de Brasil)
24-
locale-es: Español
21+
#
22+
# For convenience, the translations of locale menu options
23+
# will be the same across all locales.
24+
#
25+
# To help with maintenance, please list keys in alphabetical
26+
# order.
27+
#
28+
locale-en-US: English (US)
29+
locale-es: Spanish
30+
locale-fr-FR: Français
2531
locale-ja: 日本語
32+
locale-pt-BR: Português (do Brasil)

0 commit comments

Comments
 (0)