From 0a568c98cb0f4720e7c1269a5b912d90118c7e32 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Wed, 1 Aug 2018 16:31:06 -0400 Subject: [PATCH] LP#1770973 AngJS-compatible locale selector Signed-off-by: Bill Erickson --- Open-ILS/src/eg2/src/app/core/locale.service.ts | 67 ++++++++++++++++++++++ Open-ILS/src/eg2/src/app/staff/nav.component.html | 19 ++++++ Open-ILS/src/eg2/src/app/staff/nav.component.ts | 27 ++++++++- .../src/eg2/src/environments/environment.prod.ts | 3 +- Open-ILS/src/eg2/src/environments/environment.ts | 4 +- 5 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 Open-ILS/src/eg2/src/app/core/locale.service.ts diff --git a/Open-ILS/src/eg2/src/app/core/locale.service.ts b/Open-ILS/src/eg2/src/app/core/locale.service.ts new file mode 100644 index 0000000000..9c4ae64898 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/core/locale.service.ts @@ -0,0 +1,67 @@ +import {Injectable} from '@angular/core'; +import {Location} from '@angular/common'; +import {environment} from '../../environments/environment'; +import {Observable} from 'rxjs/Observable'; +import 'rxjs/add/observable/of' +import {CookieService} from 'ngx-cookie'; +import {IdlObject} from '@eg/core/idl.service'; +import {PcrudService} from '@eg/core/pcrud.service'; + +@Injectable({providedIn: 'root'}) +export class LocaleService { + + constructor( + private ngLocation: Location, + private cookieService: CookieService, + private pcrud: PcrudService) { + } + + setLocale(code: string) { + let url = this.ngLocation.prepareExternalUrl('/'); + + // The last part of the base path will be the locale + // Replace it with the selected locale + url = url.replace(/\/[a-z]{2}-[A-Z]{2}\/$/, `/${code}`); + + // Finally tack the path of the current page back onto the URL + // which is more friendly than forcing them back to the splash page. + url += this.ngLocation.path() + + // Set a 10-year locale cookie to maintain compatibility + // with the AngularJS client. + // Cookie takes the form aa_bb instead of aa-BB + const cookie = code.replace(/-/, '_').toLowerCase(); + this.cookieService.put('eg_locale', + cookie, {path : '/', secure: true, expires: '+10y'}); + + window.location.href = url; + } + + // Returns i18n_l objects + supportedLocaleCodes(): string[] { + return environment.locales || []; + } + + supportedLocales(): Observable { + const locales = this.supportedLocaleCodes(); + + if (locales.length === 0) { + return Observable.of(); + } + + return this.pcrud.search('i18n_l', {code: locales}); + } + + // Extract the local from the URL. + // It's the last component of the base path. + // Note we don't extract it from the cookie since using cookies + // to store the locale will not be necessary when AngularJS + // is deprecated. + currentLocaleCode(): string { + const base = this.ngLocation.prepareExternalUrl('/'); + const code = base.match(/\/([a-z]{2}-[A-Z]{2})\/$/); + return code ? code[1] : ''; + } +} + + diff --git a/Open-ILS/src/eg2/src/app/staff/nav.component.html b/Open-ILS/src/eg2/src/app/staff/nav.component.html index ad002a08de..54fb503f10 100644 --- a/Open-ILS/src/eg2/src/app/staff/nav.component.html +++ b/Open-ILS/src/eg2/src/app/staff/nav.component.html @@ -361,6 +361,25 @@ +