Skip to content

Commit

Permalink
languages adjusts (#24181)
Browse files Browse the repository at this point in the history
* languages adjusts

* update snapshots

* show languages selected choices on top.

* drop default value for languages.

* set languages value as empty array
  • Loading branch information
mshima authored Nov 13, 2023
1 parent 6051a63 commit d6e435b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 97 deletions.
33 changes: 0 additions & 33 deletions generators/app/__snapshots__/generator.spec.mts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,6 @@ exports[`generator - app with default config should match snapshot 1`] = `
"jwtSecretKey": "SECRET--64",
"languages": [
"en",
"fr",
],
"languagesDefinition": [
{
Expand All @@ -425,16 +424,6 @@ exports[`generator - app with default config should match snapshot 1`] = `
"name": "English",
"rtl": false,
},
{
"angularLocale": "fr",
"dayjsLocale": "fr",
"displayName": "Français",
"fakerjsLocale": "fr",
"javaLocaleMessageSourceSuffix": "fr",
"languageTag": "fr",
"name": "French",
"rtl": false,
},
],
"liquibaseAddH2Properties": false,
"liquibaseDefaultSchemaName": "",
Expand Down Expand Up @@ -981,7 +970,6 @@ exports[`generator - app with gateway should match snapshot 1`] = `
"jwtSecretKey": Any<String>,
"languages": [
"en",
"fr",
],
"languagesDefinition": [
{
Expand All @@ -994,16 +982,6 @@ exports[`generator - app with gateway should match snapshot 1`] = `
"name": "English",
"rtl": false,
},
{
"angularLocale": "fr",
"dayjsLocale": "fr",
"displayName": "Français",
"fakerjsLocale": "fr",
"javaLocaleMessageSourceSuffix": "fr",
"languageTag": "fr",
"name": "French",
"rtl": false,
},
],
"liquibaseAddH2Properties": false,
"liquibaseDefaultSchemaName": "",
Expand Down Expand Up @@ -1551,7 +1529,6 @@ exports[`generator - app with microservice should match snapshot 1`] = `
"jwtSecretKey": Any<String>,
"languages": [
"en",
"fr",
],
"languagesDefinition": [
{
Expand All @@ -1564,16 +1541,6 @@ exports[`generator - app with microservice should match snapshot 1`] = `
"name": "English",
"rtl": false,
},
{
"angularLocale": "fr",
"dayjsLocale": "fr",
"displayName": "Français",
"fakerjsLocale": "fr",
"javaLocaleMessageSourceSuffix": "fr",
"languageTag": "fr",
"name": "French",
"rtl": false,
},
],
"liquibaseAddH2Properties": false,
"liquibaseDefaultSchemaName": "",
Expand Down
40 changes: 0 additions & 40 deletions generators/entities/__snapshots__/generator.spec.mts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,6 @@ exports[`generator - entities regenerating all entities should match snapshot 1`
"src/main/webapp/i18n/en/skip.json": {
"stateCleared": "modified",
},
"src/main/webapp/i18n/fr/bar.json": {
"stateCleared": "modified",
},
"src/main/webapp/i18n/fr/foo.json": {
"stateCleared": "modified",
},
"src/main/webapp/i18n/fr/skip.json": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTest.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -337,31 +328,16 @@ exports[`generator - entities regenerating all entities should match source call
"translationKey": "foo",
"translationValue": "Foo",
},
{
"language": "fr",
"translationKey": "foo",
"translationValue": "Foo",
},
{
"language": "en",
"translationKey": "bar",
"translationValue": "Bar",
},
{
"language": "fr",
"translationKey": "bar",
"translationValue": "Bar",
},
{
"language": "en",
"translationKey": "skip",
"translationValue": "Skip",
},
{
"language": "fr",
"translationKey": "skip",
"translationValue": "Skip",
},
],
"addLiquibaseChangelog": [
{
Expand Down Expand Up @@ -565,12 +541,6 @@ exports[`generator - entities regenerating some entities should match snapshot 1
"src/main/webapp/i18n/en/foo.json": {
"stateCleared": "modified",
},
"src/main/webapp/i18n/fr/bar.json": {
"stateCleared": "modified",
},
"src/main/webapp/i18n/fr/foo.json": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTest.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -619,21 +589,11 @@ exports[`generator - entities regenerating some entities should match source cal
"translationKey": "foo",
"translationValue": "Foo",
},
{
"language": "fr",
"translationKey": "foo",
"translationValue": "Foo",
},
{
"language": "en",
"translationKey": "bar",
"translationValue": "Bar",
},
{
"language": "fr",
"translationKey": "bar",
"translationValue": "Bar",
},
],
"addLiquibaseChangelog": [
{
Expand Down
25 changes: 11 additions & 14 deletions generators/languages/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,24 @@ export default class LanguagesGenerator extends BaseApplicationGenerator {
}
},
defaults() {
const { nativeLanguage, languages, enableTranslation } = this.jhipsterConfigWithDefaults;
const { nativeLanguage, enableTranslation } = this.jhipsterConfigWithDefaults;
const isLanguageConfigured = Boolean(this.jhipsterConfig.nativeLanguage);
// Prompts detects current language. Save default native language for next execution.
this.config.defaults({ nativeLanguage });
if (!enableTranslation) {
if (!this.jhipsterConfig.nativeLanguage) {
this.jhipsterConfig.nativeLanguage = nativeLanguage;
}
return;
}
if (!this.jhipsterConfig.nativeLanguage) {
if (this.languagesToApply.length === 0) {
this.languagesToApply = languages;
}
this.jhipsterConfig.nativeLanguage = nativeLanguage;
}
if (!this.jhipsterConfig.languages) {
this.jhipsterConfig.languages = [];
this.config.defaults({ languages: [] });
if (!isLanguageConfigured && this.languagesToApply.length === 0) {
// If languages is not configured, apply defaults.
this.languagesToApply = this.jhipsterConfigWithDefaults.languages;
}
if (this.jhipsterConfig.languages.length === 0 || this.jhipsterConfig.languages[0] !== this.jhipsterConfig.nativeLanguage) {
this.jhipsterConfig.languages = [...new Set([nativeLanguage, ...languages])];
// Set native language as first language.
this.jhipsterConfig.languages = [...new Set([nativeLanguage, ...this.jhipsterConfig.languages])];
}
if (this.languagesToApply && this.languagesToApply.length > 0) {
// Save new languages;
// Save new languages.
this.jhipsterConfig.languages = [...new Set([...this.jhipsterConfig.languages, ...this.languagesToApply])];
}
},
Expand Down
49 changes: 47 additions & 2 deletions generators/languages/languages.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ describe('generator - languages', () => {
languages: ['en'],
}),
);
it('creates expected configuration values', () => {
runResult.assertJsonFileContent('.yo-rc.json', {
'generator-jhipster': {
languages: ['fr', 'en'],
nativeLanguage: 'fr',
enableTranslation: true,
},
});
});
containsLanguageFiles('fr');
containsLanguageFiles('en');
});
Expand All @@ -207,10 +216,19 @@ describe('generator - languages', () => {
before(() =>
helpers
.run(generatorPath)
.withLocalConfig({ enableTranslation: true, nativeLanguage: 'fr', languages: ['en', 'fr'] })
.withJHipsterConfig({ enableTranslation: true, nativeLanguage: 'fr', languages: ['en', 'fr'] })
.withOptions({ ignoreNeedlesError: true })
.withOptions({ skipPrompts: true, regenerate: true, baseName: 'jhipster' }),
);
it('creates expected configuration values', () => {
runResult.assertJsonFileContent('.yo-rc.json', {
'generator-jhipster': {
languages: ['fr', 'en'],
nativeLanguage: 'fr',
enableTranslation: true,
},
});
});
containsLanguageFiles('fr');
containsLanguageFiles('en');
});
Expand All @@ -234,11 +252,20 @@ describe('generator - languages', () => {
before(() =>
helpers
.run(generatorPath)
.withLocalConfig({ enableTranslation: true, nativeLanguage: 'en' })
.withJHipsterConfig({ enableTranslation: true, nativeLanguage: 'en' })
.withOptions({ ignoreNeedlesError: true })
.withArguments(['fr', 'de'])
.withOptions({ baseName: 'jhipster' }),
);
it('creates expected configuration values', () => {
runResult.assertJsonFileContent('.yo-rc.json', {
'generator-jhipster': {
languages: ['en', 'fr', 'de'],
nativeLanguage: 'en',
enableTranslation: true,
},
});
});
containsLanguageFiles('fr');
containsLanguageFiles('de');
});
Expand All @@ -262,6 +289,15 @@ describe('generator - languages', () => {
})
.run();
});
it('creates expected configuration values', () => {
runResult.assertJsonFileContent('.yo-rc.json', {
'generator-jhipster': {
languages: ['en', 'fr', 'de'],
nativeLanguage: 'en',
enableTranslation: true,
},
});
});
describe('for native language translation', () => {
containsLanguageFiles('en');
containsLanguageInVueStore('en');
Expand All @@ -283,6 +319,15 @@ describe('generator - languages', () => {
});
await result.create('jhipster:languages').withArguments(['fr', 'de']).withOptions({ baseName: 'jhipster' }).run();
});
it('creates expected configuration values', () => {
runResult.assertJsonFileContent('.yo-rc.json', {
'generator-jhipster': {
languages: ['en', 'fr', 'de'],
nativeLanguage: 'en',
enableTranslation: true,
},
});
});
describe('for native language translation', () => {
containsLanguageFiles('en');
containsLanguageInVueStore('en');
Expand Down
4 changes: 3 additions & 1 deletion generators/languages/prompts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export async function askForLanguages({ control }) {
choices: () => {
const languageOptions = this.supportedLanguages;
const nativeLanguage = this.jhipsterConfigWithDefaults.nativeLanguage;
return languagesAsChoices(languageOptions.filter(l => l.languageTag !== nativeLanguage));
const choices = languagesAsChoices(languageOptions.filter(l => l.languageTag !== nativeLanguage));
const defaults = this.jhipsterConfigWithDefaults.languages ?? [];
return [...choices.filter(({ value }) => defaults.includes(value)), ...choices.filter(({ value }) => !defaults.includes(value))];
},
default: () => this.jhipsterConfigWithDefaults.languages,
},
Expand Down
6 changes: 0 additions & 6 deletions generators/server/__snapshots__/generator.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1061,9 +1061,6 @@ exports[`generator - server with entities should match files snapshot 1`] = `
"src/main/resources/i18n/messages_en.properties": {
"stateCleared": "modified",
},
"src/main/resources/i18n/messages_fr.properties": {
"stateCleared": "modified",
},
"src/main/resources/logback-spring.xml": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1211,9 +1208,6 @@ exports[`generator - server with entities should match files snapshot 1`] = `
"src/test/resources/i18n/messages_en.properties": {
"stateCleared": "modified",
},
"src/test/resources/i18n/messages_fr.properties": {
"stateCleared": "modified",
},
"src/test/resources/junit-platform.properties": {
"stateCleared": "modified",
},
Expand Down
2 changes: 1 addition & 1 deletion jdl/jhipster/default-application-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export function getConfigForTranslation(options: any = {}): any {
options[NATIVE_LANGUAGE] = 'en';
}
if (options[ENABLE_TRANSLATION] && options[LANGUAGES] === undefined) {
options[LANGUAGES] = ['en', 'fr'];
options[LANGUAGES] = [];
}
return options;
}
Expand Down

0 comments on commit d6e435b

Please sign in to comment.