Commit 396250da by Tuukka Kivilahti

Merge branch 'feature/i18n-for-angular' of gitlab.codecrew.fi:codecrew/moya into…

… feature/i18n-for-angular
2 parents 3e5da1d0 05a1b193
Pipeline #39 running
in 0 seconds
...@@ -2,13 +2,10 @@ import { Component, OnInit } from '@angular/core'; ...@@ -2,13 +2,10 @@ import { Component, OnInit } from '@angular/core';
import {DEFAULT_LOCALE, MoyaLocaleService} from './moya-locale.service'; import {DEFAULT_LOCALE, MoyaLocaleService} from './moya-locale.service';
import {TranslateService} from '@ngx-translate/core'; import {TranslateService} from '@ngx-translate/core';
const ENGLISH = 'en'; const ENGLISH = 'en';
const FINNISH = 'fi'; const FINNISH = 'fi';
const SWEDISH = 'sv'; const SWEDISH = 'sv';
@Component({ @Component({
selector: 'moya-locale', selector: 'moya-locale',
templateUrl: './moya-locale.component.html', templateUrl: './moya-locale.component.html',
...@@ -30,7 +27,6 @@ export class MoyaLocaleComponent implements OnInit { ...@@ -30,7 +27,6 @@ export class MoyaLocaleComponent implements OnInit {
this.localeService.getUserLocale().subscribe(locale => { this.selectLocale(locale); } ); this.localeService.getUserLocale().subscribe(locale => { this.selectLocale(locale); } );
} }
selectLocale(locale: string, save = false): void { selectLocale(locale: string, save = false): void {
if (![ENGLISH, FINNISH, SWEDISH].includes(locale)) { if (![ENGLISH, FINNISH, SWEDISH].includes(locale)) {
...@@ -59,7 +55,6 @@ export class MoyaLocaleComponent implements OnInit { ...@@ -59,7 +55,6 @@ export class MoyaLocaleComponent implements OnInit {
} }
selectEnglish() { selectEnglish() {
this.selectLocale(ENGLISH, true); this.selectLocale(ENGLISH, true);
} }
...@@ -71,8 +66,4 @@ export class MoyaLocaleComponent implements OnInit { ...@@ -71,8 +66,4 @@ export class MoyaLocaleComponent implements OnInit {
selectSwedish() { selectSwedish() {
this.selectLocale(SWEDISH, true); this.selectLocale(SWEDISH, true);
} }
} }
export interface MoyaLocale {
userLocale: string;
export class MoyaLocale { eventLocale: string;
public userLocale?: string;
public eventLocale?: string;
constructor() { }
} }
...@@ -8,14 +8,10 @@ import 'rxjs/add/operator/catch'; ...@@ -8,14 +8,10 @@ import 'rxjs/add/operator/catch';
export const DEFAULT_LOCALE = 'fi'; export const DEFAULT_LOCALE = 'fi';
const LOCALSTORAGE_NAME = 'currently used locale code'; const LOCALSTORAGE_NAME = 'currently used locale code';
@Injectable() @Injectable()
export class MoyaLocaleService { export class MoyaLocaleService {
constructor(private http: HttpClient) { } constructor(private http: HttpClient) { }
/** /**
* Order of Locale: * Order of Locale:
* 1. User's locale from database * 1. User's locale from database
...@@ -27,24 +23,25 @@ export class MoyaLocaleService { ...@@ -27,24 +23,25 @@ export class MoyaLocaleService {
*/ */
public getUserLocale(): Observable<string> { public getUserLocale(): Observable<string> {
return this.http.get<MoyaLocale>(MOYA_BASE_URL + '/v3/locale/').catch(x => Observable.of(new MoyaLocale())).map(locale => { return this.http.get<MoyaLocale>(MOYA_BASE_URL + '/v3/locale/')
.catch(x => Observable.of({} as MoyaLocale))
.map(locale => {
if (locale && locale.userLocale) {
return locale.userLocale;
}
if (locale && locale.userLocale) { const storageLocale = localStorage.getItem(LOCALSTORAGE_NAME);
return locale.userLocale;
}
const storageLocale = localStorage.getItem(LOCALSTORAGE_NAME); if (storageLocale) {
return storageLocale;
}
if (storageLocale) { if (locale.eventLocale) {
return storageLocale; return locale.eventLocale;
} }
if (locale.eventLocale) { return DEFAULT_LOCALE;
return locale.eventLocale; });
}
return DEFAULT_LOCALE;
});
} }
...@@ -53,20 +50,18 @@ export class MoyaLocaleService { ...@@ -53,20 +50,18 @@ export class MoyaLocaleService {
* *
* There is no errors * There is no errors
* @param {string} locale * @param {string} locale
* @return {Promise<any>} This will return promise, but it will contain nothing.
*/ */
public setUserLocale(locale: string): void { public setUserLocale(locale: string): void {
const newLocale: MoyaLocale = new MoyaLocale(); const newLocale: MoyaLocale = {
userLocale: locale,
newLocale.userLocale = locale; eventLocale: undefined
};
// let's save locale to database, if it fails, we save it into localstorage. No errors to show for user. // let's save locale to database, if it fails, we save it into localstorage. No errors to show for user.
this.http.post(MOYA_BASE_URL + '/v3/locale/', newLocale).first().catch(x => { this.http.post(MOYA_BASE_URL + '/v3/locale/', newLocale).catch(x => {
localStorage.setItem(LOCALSTORAGE_NAME, locale); localStorage.setItem(LOCALSTORAGE_NAME, locale);
return 'ok'; return 'ok';
}).subscribe(); }).subscribe();
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!