-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Edit Project Info): Multiple languages setting (#1546)
Allow authors to select default language and supported languages
- Loading branch information
1 parent
63f86ee
commit 6eaebf0
Showing
17 changed files
with
273 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,12 @@ | ||
export const localeToLanguage = { | ||
export const localeToLanguage: { [locale: string]: string } = { | ||
zh_CN: $localize`Chinese (Simplified)`, | ||
zh_TW: $localize`Chinese (Traditional)`, | ||
nl: $localize`Dutch`, | ||
en_US: $localize`English`, | ||
es: $localize`Spanish`, | ||
de: $localize`German`, | ||
it: $localize`Italian`, | ||
ja: $localize`Japanese`, | ||
it: $localize`Italian` | ||
ko: $localize`Korean`, | ||
es: $localize`Spanish`, | ||
vi: $localize`Vietnamese` | ||
}; |
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 |
---|---|---|
@@ -1,39 +1,52 @@ | ||
import { Language } from './language'; | ||
import { localeToLanguage } from './localeToLanguage'; | ||
|
||
const defaultLocales = { | ||
default: 'en_US', | ||
supported: [] | ||
}; | ||
export class ProjectLocale { | ||
private default: string; | ||
private supported: string[]; | ||
private locale: { default: string; supported: string[] }; | ||
|
||
constructor(locales: any = defaultLocales) { | ||
this.default = locales.default; | ||
this.supported = locales.supported; | ||
constructor(locale: any) { | ||
this.locale = locale; | ||
} | ||
|
||
getAvailableLanguages(): Language[] { | ||
return [this.getDefaultLanguage()].concat(this.getSupportedLanguages()); | ||
} | ||
|
||
getDefaultLanguage(): Language { | ||
return { language: localeToLanguage[this.locale.default], locale: this.locale.default }; | ||
} | ||
|
||
setDefaultLocale(locale: string): void { | ||
this.locale.default = locale; | ||
this.locale.supported = this.locale.supported.filter( | ||
(supportedLocale) => supportedLocale != locale | ||
); | ||
} | ||
|
||
getSupportedLanguages(): Language[] { | ||
return this.supported.map((locale) => ({ | ||
return this.locale.supported.map((locale) => ({ | ||
language: localeToLanguage[locale], | ||
locale: locale | ||
})); | ||
} | ||
|
||
setSupportedLanguages(languages: Language[]): void { | ||
this.locale.supported = languages.map((language) => language.locale); | ||
} | ||
|
||
private hasLocale(locale: string): boolean { | ||
return this.supported.includes(locale); | ||
return this.locale.supported.includes(locale); | ||
} | ||
|
||
hasTranslations(): boolean { | ||
return this.supported.length > 1; | ||
return this.locale.supported.length > 0; | ||
} | ||
|
||
hasTranslationsToApply(locale: string): boolean { | ||
return !this.isDefaultLocale(locale) && this.hasLocale(locale); | ||
} | ||
|
||
isDefaultLocale(locale: string): boolean { | ||
return this.default === locale; | ||
return this.locale.default === locale; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/app/services/sampleData/curriculum/TeacherProjectServiceSpec.project.json
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"startGroupId": "group0", | ||
"startNodeId": "node1", | ||
"metadata": {}, | ||
"nodes": [ | ||
{ | ||
"id": "group0", | ||
|
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
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
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
...l/project-info/edit-project-language-setting/edit-project-language-setting.component.html
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 @@ | ||
<h3 i18n>Language</h3> | ||
<span i18n>Default language:</span> | ||
<ng-select | ||
[items]="availableLanguages" | ||
bindLabel="language" | ||
[(ngModel)]="defaultLanguage" | ||
[clearable]="false" | ||
(change)="updateDefaultLanguage()" | ||
> | ||
</ng-select> | ||
<span i18n>Additional languages:</span> | ||
<ng-select | ||
[items]="availableLanguages" | ||
bindLabel="language" | ||
[(ngModel)]="supportedLanguages" | ||
[closeOnSelect]="false" | ||
[multiple]="true" | ||
(change)="updateSupportedLanguages()" | ||
> | ||
</ng-select> |
Empty file.
33 changes: 33 additions & 0 deletions
33
...roject-info/edit-project-language-setting/edit-project-language-setting.component.spec.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,33 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { NgSelectModule } from '@ng-select/ng-select'; | ||
import { EditProjectLanguageSettingComponent } from './edit-project-language-setting.component'; | ||
import { TeacherProjectService } from '../../../services/teacherProjectService'; | ||
import { ProjectLocale } from '../../../../../app/domain/projectLocale'; | ||
import { FormsModule } from '@angular/forms'; | ||
|
||
class MockTeacherProjectService { | ||
getLocale() {} | ||
saveProject() {} | ||
} | ||
describe('EditProjectLanguageSettingComponent', () => { | ||
let component: EditProjectLanguageSettingComponent; | ||
let fixture: ComponentFixture<EditProjectLanguageSettingComponent>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [EditProjectLanguageSettingComponent], | ||
imports: [FormsModule, NgSelectModule], | ||
providers: [{ provide: TeacherProjectService, useClass: MockTeacherProjectService }] | ||
}); | ||
fixture = TestBed.createComponent(EditProjectLanguageSettingComponent); | ||
component = fixture.componentInstance; | ||
spyOn(TestBed.inject(TeacherProjectService), 'getLocale').and.returnValue( | ||
new ProjectLocale({ default: 'en_US', supported: [] }) | ||
); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('creates', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
...ool/project-info/edit-project-language-setting/edit-project-language-setting.component.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,46 @@ | ||
import { Component } from '@angular/core'; | ||
import { Language } from '../../../../../app/domain/language'; | ||
import { TeacherProjectService } from '../../../services/teacherProjectService'; | ||
import { ProjectLocale } from '../../../../../app/domain/projectLocale'; | ||
import { localeToLanguage } from '../../../../../app/domain/localeToLanguage'; | ||
|
||
@Component({ | ||
selector: 'edit-project-language-setting', | ||
templateUrl: './edit-project-language-setting.component.html' | ||
}) | ||
export class EditProjectLanguageSettingComponent { | ||
protected availableLanguages: Language[]; | ||
protected defaultLanguage: Language; | ||
private projectLocale: ProjectLocale; | ||
protected supportedLanguages: Language[]; | ||
|
||
constructor(private projectService: TeacherProjectService) {} | ||
|
||
ngOnInit(): void { | ||
this.updateModel(); | ||
} | ||
|
||
private updateModel(): void { | ||
this.projectLocale = this.projectService.getLocale(); | ||
this.defaultLanguage = this.projectLocale.getDefaultLanguage(); | ||
this.supportedLanguages = this.projectLocale.getSupportedLanguages(); | ||
this.availableLanguages = Object.entries(localeToLanguage) | ||
.map(([locale, language]) => ({ | ||
locale: locale, | ||
language: language | ||
})) | ||
.filter((language) => language.locale != this.defaultLanguage.locale); | ||
} | ||
|
||
protected updateDefaultLanguage(): void { | ||
this.projectLocale.setDefaultLocale(this.defaultLanguage.locale); | ||
this.projectService.saveProject(); | ||
this.updateModel(); | ||
} | ||
|
||
protected updateSupportedLanguages(): void { | ||
this.projectLocale.setSupportedLanguages(this.supportedLanguages); | ||
this.projectService.saveProject(); | ||
this.updateModel(); | ||
} | ||
} |
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
Oops, something went wrong.