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';
import {DEFAULT_LOCALE, MoyaLocaleService} from './moya-locale.service';
import {TranslateService} from '@ngx-translate/core';
const ENGLISH = 'en';
const FINNISH = 'fi';
const SWEDISH = 'sv';
@Component({
selector: 'moya-locale',
templateUrl: './moya-locale.component.html',
......@@ -30,7 +27,6 @@ export class MoyaLocaleComponent implements OnInit {
this.localeService.getUserLocale().subscribe(locale => { this.selectLocale(locale); } );
}
selectLocale(locale: string, save = false): void {
if (![ENGLISH, FINNISH, SWEDISH].includes(locale)) {
......@@ -59,7 +55,6 @@ export class MoyaLocaleComponent implements OnInit {
}
selectEnglish() {
this.selectLocale(ENGLISH, true);
}
......@@ -71,8 +66,4 @@ export class MoyaLocaleComponent implements OnInit {
selectSwedish() {
this.selectLocale(SWEDISH, true);
}
}
export class MoyaLocale {
public userLocale?: string;
public eventLocale?: string;
constructor() { }
export interface MoyaLocale {
userLocale: string;
eventLocale: string;
}
......@@ -8,14 +8,10 @@ import 'rxjs/add/operator/catch';
export const DEFAULT_LOCALE = 'fi';
const LOCALSTORAGE_NAME = 'currently used locale code';
@Injectable()
export class MoyaLocaleService {
constructor(private http: HttpClient) { }
/**
* Order of Locale:
* 1. User's locale from database
......@@ -27,8 +23,9 @@ export class MoyaLocaleService {
*/
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;
}
......@@ -53,20 +50,18 @@ export class MoyaLocaleService {
*
* There is no errors
* @param {string} locale
* @return {Promise<any>} This will return promise, but it will contain nothing.
*/
public setUserLocale(locale: string): void {
const newLocale: MoyaLocale = new MoyaLocale();
newLocale.userLocale = locale;
const newLocale: MoyaLocale = {
userLocale: locale,
eventLocale: undefined
};
// 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);
return 'ok';
}).subscribe();
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!