diff options
author | Chocobozzz <me@florianbigard.com> | 2018-06-06 16:46:42 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-06-06 16:48:41 +0200 |
commit | 7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac (patch) | |
tree | 6f178426c165f9136eb08354efa4a06c24725f87 /client/src/app/core/server | |
parent | f07d6385b4b46c3254898292a8a53ed415b8d49b (diff) | |
download | PeerTube-7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac.tar.gz PeerTube-7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac.tar.zst PeerTube-7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac.zip |
Add server localization
Diffstat (limited to 'client/src/app/core/server')
-rw-r--r-- | client/src/app/core/server/server.service.ts | 76 |
1 files changed, 52 insertions, 24 deletions
diff --git a/client/src/app/core/server/server.service.ts b/client/src/app/core/server/server.service.ts index ccae5a151..56d33339e 100644 --- a/client/src/app/core/server/server.service.ts +++ b/client/src/app/core/server/server.service.ts | |||
@@ -1,17 +1,20 @@ | |||
1 | import { tap } from 'rxjs/operators' | 1 | import { map, share, switchMap, tap } from 'rxjs/operators' |
2 | import { HttpClient } from '@angular/common/http' | 2 | import { HttpClient } from '@angular/common/http' |
3 | import { Injectable } from '@angular/core' | 3 | import { Inject, Injectable, LOCALE_ID } from '@angular/core' |
4 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' | 4 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' |
5 | import { ReplaySubject } from 'rxjs' | 5 | import { Observable, ReplaySubject } from 'rxjs' |
6 | import { ServerConfig } from '../../../../../shared' | 6 | import { ServerConfig } from '../../../../../shared' |
7 | import { About } from '../../../../../shared/models/server/about.model' | 7 | import { About } from '../../../../../shared/models/server/about.model' |
8 | import { environment } from '../../../environments/environment' | 8 | import { environment } from '../../../environments/environment' |
9 | import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos' | 9 | import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos' |
10 | import { buildFileLocale, getDefaultLocale } from '../../../../../shared/models/i18n' | ||
11 | import { peertubeTranslate } from '@app/shared/i18n/i18n-utils' | ||
10 | 12 | ||
11 | @Injectable() | 13 | @Injectable() |
12 | export class ServerService { | 14 | export class ServerService { |
13 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/' | 15 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/' |
14 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | 16 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' |
17 | private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/' | ||
15 | private static CONFIG_LOCAL_STORAGE_KEY = 'server-config' | 18 | private static CONFIG_LOCAL_STORAGE_KEY = 'server-config' |
16 | 19 | ||
17 | configLoaded = new ReplaySubject<boolean>(1) | 20 | configLoaded = new ReplaySubject<boolean>(1) |
@@ -19,6 +22,7 @@ export class ServerService { | |||
19 | videoCategoriesLoaded = new ReplaySubject<boolean>(1) | 22 | videoCategoriesLoaded = new ReplaySubject<boolean>(1) |
20 | videoLicencesLoaded = new ReplaySubject<boolean>(1) | 23 | videoLicencesLoaded = new ReplaySubject<boolean>(1) |
21 | videoLanguagesLoaded = new ReplaySubject<boolean>(1) | 24 | videoLanguagesLoaded = new ReplaySubject<boolean>(1) |
25 | localeObservable: Observable<any> | ||
22 | 26 | ||
23 | private config: ServerConfig = { | 27 | private config: ServerConfig = { |
24 | instance: { | 28 | instance: { |
@@ -64,8 +68,12 @@ export class ServerService { | |||
64 | private videoLanguages: Array<VideoConstant<string>> = [] | 68 | private videoLanguages: Array<VideoConstant<string>> = [] |
65 | private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = [] | 69 | private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = [] |
66 | 70 | ||
67 | constructor (private http: HttpClient) { | 71 | constructor ( |
72 | private http: HttpClient, | ||
73 | @Inject(LOCALE_ID) private localeId: string | ||
74 | ) { | ||
68 | this.loadConfigLocally() | 75 | this.loadConfigLocally() |
76 | this.loadServerLocale() | ||
69 | } | 77 | } |
70 | 78 | ||
71 | loadConfig () { | 79 | loadConfig () { |
@@ -124,26 +132,46 @@ export class ServerService { | |||
124 | notifier: ReplaySubject<boolean>, | 132 | notifier: ReplaySubject<boolean>, |
125 | sort = false | 133 | sort = false |
126 | ) { | 134 | ) { |
127 | return this.http.get(ServerService.BASE_VIDEO_URL + attributeName) | 135 | this.localeObservable |
128 | .subscribe(data => { | 136 | .pipe( |
129 | Object.keys(data) | 137 | switchMap(translations => { |
130 | .forEach(dataKey => { | 138 | return this.http.get(ServerService.BASE_VIDEO_URL + attributeName) |
131 | hashToPopulate.push({ | 139 | .pipe(map(data => ({ data, translations }))) |
132 | id: dataKey, | 140 | }) |
133 | label: data[dataKey] | 141 | ) |
134 | }) | 142 | .subscribe(({ data, translations }) => { |
135 | }) | 143 | Object.keys(data) |
136 | 144 | .forEach(dataKey => { | |
137 | if (sort === true) { | 145 | const label = data[ dataKey ] |
138 | hashToPopulate.sort((a, b) => { | 146 | |
139 | if (a.label < b.label) return -1 | 147 | hashToPopulate.push({ |
140 | if (a.label === b.label) return 0 | 148 | id: dataKey, |
141 | return 1 | 149 | label: peertubeTranslate(label, translations) |
142 | }) | 150 | }) |
143 | } | 151 | }) |
144 | 152 | ||
145 | notifier.next(true) | 153 | if (sort === true) { |
146 | }) | 154 | hashToPopulate.sort((a, b) => { |
155 | if (a.label < b.label) return -1 | ||
156 | if (a.label === b.label) return 0 | ||
157 | return 1 | ||
158 | }) | ||
159 | } | ||
160 | |||
161 | notifier.next(true) | ||
162 | }) | ||
163 | } | ||
164 | |||
165 | private loadServerLocale () { | ||
166 | const fileLocale = buildFileLocale(environment.production === true ? this.localeId : 'fr') | ||
167 | |||
168 | // Default locale, nothing to translate | ||
169 | const defaultFileLocale = buildFileLocale(getDefaultLocale()) | ||
170 | if (fileLocale === defaultFileLocale) return {} | ||
171 | |||
172 | this.localeObservable = this.http | ||
173 | .get(ServerService.BASE_LOCALE_URL + fileLocale + '/server.json') | ||
174 | .pipe(share()) | ||
147 | } | 175 | } |
148 | 176 | ||
149 | private saveConfigLocally (config: ServerConfig) { | 177 | private saveConfigLocally (config: ServerConfig) { |