From d009b5c500abecdc15fdf0b59c126d89b661ab65 Mon Sep 17 00:00:00 2001
From: Florent Fortat <florent.fortat@tralalere.com>
Date: Thu, 18 Jan 2018 18:38:36 +0100
Subject: [PATCH] Add support for localized API urls

---
 src/data-manager.service.ts                                 | 10 ++++++++++
 src/external-interface/drupal-interface-config.interface.ts |  1 +
 src/external-interface/drupal-interface.class.ts            | 10 ++++++++++
 src/external-interface/external-interface.interface.ts      |  1 +
 src/external-interface/local-storage-interface.class.ts     |  2 ++
 src/external-interface/nodejs-interface.class.ts            |  2 ++
 6 files changed, 26 insertions(+)

diff --git a/src/data-manager.service.ts b/src/data-manager.service.ts
index e257b27..f1a5f0e 100644
--- a/src/data-manager.service.ts
+++ b/src/data-manager.service.ts
@@ -82,6 +82,16 @@ export class DataManagerService {
         }
     }
 
+    useLocale(locale:string) {
+        this.entitiesCollectionsCache = {};
+        this.entitiesCollectionsSubjects = {};
+        this.entitiesSubjects = {};
+
+        for (let interfaceId in this.interfaces) {
+            this.interfaces[interfaceId].useLocale(locale);
+        }
+    }
+
 
     getInterface(endPointName:string):ExternalInterface {
         var interfaceId:string;
diff --git a/src/external-interface/drupal-interface-config.interface.ts b/src/external-interface/drupal-interface-config.interface.ts
index 32e1196..05edf2c 100644
--- a/src/external-interface/drupal-interface-config.interface.ts
+++ b/src/external-interface/drupal-interface-config.interface.ts
@@ -3,4 +3,5 @@
  */
 export interface DrupalInterfaceConfig {
     apiUrl:string;
+    localeUrls?:Object;
 }
\ No newline at end of file
diff --git a/src/external-interface/drupal-interface.class.ts b/src/external-interface/drupal-interface.class.ts
index aea27ba..6fc2a96 100644
--- a/src/external-interface/drupal-interface.class.ts
+++ b/src/external-interface/drupal-interface.class.ts
@@ -16,6 +16,8 @@ import 'rxjs/add/operator/map';
 
 export class DrupalInterface implements ExternalInterface {
 
+    private locale:string;
+
     constructor(
         protected http:Http,
         protected manager:DataManagerService,
@@ -31,8 +33,16 @@ export class DrupalInterface implements ExternalInterface {
        return Settings.getHeaders();
     }
 
+    useLocale(locale:string) {
+        this.locale = locale;
+    }
+
 
     getApiUrl(entityType:string):string {
+        if (this.locale && this.configuration.localeUrls[this.locale]) {
+            return this.configuration.localeUrls[this.locale];
+        }
+
         return this.configuration.apiUrl;
     }
 
diff --git a/src/external-interface/external-interface.interface.ts b/src/external-interface/external-interface.interface.ts
index ac83bf4..6018f23 100644
--- a/src/external-interface/external-interface.interface.ts
+++ b/src/external-interface/external-interface.interface.ts
@@ -8,6 +8,7 @@ import {Response} from "@angular/http";
 
 export interface ExternalInterface {
 
+    useLocale(locale: string);
     getEntity(entityType: string): Observable<DataEntity>;
 
     loadEntity(entityType: string, entityId: any): Observable<DataEntity>;
diff --git a/src/external-interface/local-storage-interface.class.ts b/src/external-interface/local-storage-interface.class.ts
index c81c40c..5e537f9 100644
--- a/src/external-interface/local-storage-interface.class.ts
+++ b/src/external-interface/local-storage-interface.class.ts
@@ -23,6 +23,8 @@ export class LocalStorageInterface implements ExternalInterface {
         this.loadIndex();
     }
 
+    useLocale(locale: string) { }
+
     saveIndex() {
         localStorage["max-index"] = String(this.maxIndex);
     }
diff --git a/src/external-interface/nodejs-interface.class.ts b/src/external-interface/nodejs-interface.class.ts
index 5d03d2c..fb25cdc 100644
--- a/src/external-interface/nodejs-interface.class.ts
+++ b/src/external-interface/nodejs-interface.class.ts
@@ -58,6 +58,8 @@ export class NodeJsInterface implements ExternalInterface {
         }
     }
 
+    useLocale(locale: string) { }
+
     init(type) {
         this.messageSubject = new ReplaySubject<NodeJsDataInterface[]>(1);
     }
-- 
2.16.0.windows.2

