aboutsummaryrefslogtreecommitdiffhomepage
path: root/client
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-06-06 17:37:13 +0200
committerChocobozzz <me@florianbigard.com>2018-06-06 17:37:13 +0200
commit74b7c6d48e9ca377fe938c8134ed74b612e62ba0 (patch)
treecb848dec054a71669fef5ea3986bdd0d2c354248 /client
parent7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac (diff)
downloadPeerTube-74b7c6d48e9ca377fe938c8134ed74b612e62ba0.tar.gz
PeerTube-74b7c6d48e9ca377fe938c8134ed74b612e62ba0.tar.zst
PeerTube-74b7c6d48e9ca377fe938c8134ed74b612e62ba0.zip
Little i18n refractoring
Diffstat (limited to 'client')
-rw-r--r--client/src/app/app.module.ts18
-rw-r--r--client/src/app/core/server/server.service.ts20
-rw-r--r--client/src/app/shared/i18n/i18n-utils.ts12
-rw-r--r--client/src/app/shared/video/video.service.ts4
-rw-r--r--client/src/app/videos/+video-watch/video-watch.component.ts3
-rw-r--r--client/src/assets/player/peertube-player.ts10
-rw-r--r--client/src/main.ts7
7 files changed, 47 insertions, 27 deletions
diff --git a/client/src/app/app.module.ts b/client/src/app/app.module.ts
index e60a74cc0..51e354378 100644
--- a/client/src/app/app.module.ts
+++ b/client/src/app/app.module.ts
@@ -16,8 +16,8 @@ import { MenuComponent } from './menu'
16import { SharedModule } from './shared' 16import { SharedModule } from './shared'
17import { SignupModule } from './signup' 17import { SignupModule } from './signup'
18import { VideosModule } from './videos' 18import { VideosModule } from './videos'
19import { buildFileLocale, getDefaultLocale } from '../../../shared/models/i18n' 19import { buildFileLocale, getCompleteLocale, getDefaultLocale, isDefaultLocale } from '../../../shared/models/i18n'
20import { environment } from '../environments/environment' 20import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
21 21
22export function metaFactory (serverService: ServerService): MetaLoader { 22export function metaFactory (serverService: ServerService): MetaLoader {
23 return new MetaStaticLoader({ 23 return new MetaStaticLoader({
@@ -67,17 +67,17 @@ export function metaFactory (serverService: ServerService): MetaLoader {
67 { 67 {
68 provide: TRANSLATIONS, 68 provide: TRANSLATIONS,
69 useFactory: (locale) => { 69 useFactory: (locale) => {
70 // On dev mode, test locales 70 // On dev mode, test localization
71 if (environment.production === false && window.location.search === '?lang=fr') { 71 if (isOnDevLocale()) {
72 return require(`raw-loader!../locale/target/angular_fr.xml`) 72 locale = getDevLocale()
73 return require(`raw-loader!../locale/target/angular_${locale}.xml`)
73 } 74 }
74 75
75 const fileLocale = buildFileLocale(locale)
76
77 // Default locale, nothing to translate 76 // Default locale, nothing to translate
78 const defaultFileLocale = buildFileLocale(getDefaultLocale()) 77 const completeLocale = getCompleteLocale(locale)
79 if (fileLocale === defaultFileLocale) return '' 78 if (isDefaultLocale(completeLocale)) return ''
80 79
80 const fileLocale = buildFileLocale(locale)
81 return require(`raw-loader!../locale/target/angular_${fileLocale}.xml`) 81 return require(`raw-loader!../locale/target/angular_${fileLocale}.xml`)
82 }, 82 },
83 deps: [ LOCALE_ID ] 83 deps: [ LOCALE_ID ]
diff --git a/client/src/app/core/server/server.service.ts b/client/src/app/core/server/server.service.ts
index 56d33339e..74363e6a1 100644
--- a/client/src/app/core/server/server.service.ts
+++ b/client/src/app/core/server/server.service.ts
@@ -2,13 +2,13 @@ import { map, share, switchMap, tap } from 'rxjs/operators'
2import { HttpClient } from '@angular/common/http' 2import { HttpClient } from '@angular/common/http'
3import { Inject, Injectable, LOCALE_ID } from '@angular/core' 3import { Inject, Injectable, LOCALE_ID } from '@angular/core'
4import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' 4import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
5import { Observable, ReplaySubject } from 'rxjs' 5import { Observable, ReplaySubject, of } from 'rxjs'
6import { ServerConfig } from '../../../../../shared' 6import { getCompleteLocale, ServerConfig } from '../../../../../shared'
7import { About } from '../../../../../shared/models/server/about.model' 7import { About } from '../../../../../shared/models/server/about.model'
8import { environment } from '../../../environments/environment' 8import { environment } from '../../../environments/environment'
9import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos' 9import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
10import { buildFileLocale, getDefaultLocale } from '../../../../../shared/models/i18n' 10import { isDefaultLocale } from '../../../../../shared/models/i18n'
11import { peertubeTranslate } from '@app/shared/i18n/i18n-utils' 11import { getDevLocale, isOnDevLocale, peertubeTranslate } from '@app/shared/i18n/i18n-utils'
12 12
13@Injectable() 13@Injectable()
14export class ServerService { 14export class ServerService {
@@ -72,8 +72,8 @@ export class ServerService {
72 private http: HttpClient, 72 private http: HttpClient,
73 @Inject(LOCALE_ID) private localeId: string 73 @Inject(LOCALE_ID) private localeId: string
74 ) { 74 ) {
75 this.loadConfigLocally()
76 this.loadServerLocale() 75 this.loadServerLocale()
76 this.loadConfigLocally()
77 } 77 }
78 78
79 loadConfig () { 79 loadConfig () {
@@ -163,14 +163,16 @@ export class ServerService {
163 } 163 }
164 164
165 private loadServerLocale () { 165 private loadServerLocale () {
166 const fileLocale = buildFileLocale(environment.production === true ? this.localeId : 'fr') 166 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
167 167
168 // Default locale, nothing to translate 168 // Default locale, nothing to translate
169 const defaultFileLocale = buildFileLocale(getDefaultLocale()) 169 if (isDefaultLocale(completeLocale)) {
170 if (fileLocale === defaultFileLocale) return {} 170 this.localeObservable = of({}).pipe(share())
171 return
172 }
171 173
172 this.localeObservable = this.http 174 this.localeObservable = this.http
173 .get(ServerService.BASE_LOCALE_URL + fileLocale + '/server.json') 175 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
174 .pipe(share()) 176 .pipe(share())
175 } 177 }
176 178
diff --git a/client/src/app/shared/i18n/i18n-utils.ts b/client/src/app/shared/i18n/i18n-utils.ts
index c1de51b7b..37180b930 100644
--- a/client/src/app/shared/i18n/i18n-utils.ts
+++ b/client/src/app/shared/i18n/i18n-utils.ts
@@ -1,7 +1,19 @@
1import { environment } from '../../../environments/environment'
2
1function peertubeTranslate (str: string, translations: { [ id: string ]: string }) { 3function peertubeTranslate (str: string, translations: { [ id: string ]: string }) {
2 return translations[str] ? translations[str] : str 4 return translations[str] ? translations[str] : str
3} 5}
4 6
7function isOnDevLocale () {
8 return environment.production === false && window.location.search === '?lang=fr'
9}
10
11function getDevLocale () {
12 return 'fr'
13}
14
5export { 15export {
16 getDevLocale,
17 isOnDevLocale,
6 peertubeTranslate 18 peertubeTranslate
7} 19}
diff --git a/client/src/app/shared/video/video.service.ts b/client/src/app/shared/video/video.service.ts
index c607b7d6a..58cb52efc 100644
--- a/client/src/app/shared/video/video.service.ts
+++ b/client/src/app/shared/video/video.service.ts
@@ -46,8 +46,8 @@ export class VideoService {
46 return this.serverService.localeObservable 46 return this.serverService.localeObservable
47 .pipe( 47 .pipe(
48 switchMap(translations => { 48 switchMap(translations => {
49 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid) 49 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
50 .pipe(map(videoHash => ({ videoHash, translations }))) 50 .pipe(map(videoHash => ({ videoHash, translations })))
51 }), 51 }),
52 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)), 52 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
53 catchError(res => this.restExtractor.handleError(res)) 53 catchError(res => this.restExtractor.handleError(res))
diff --git a/client/src/app/videos/+video-watch/video-watch.component.ts b/client/src/app/videos/+video-watch/video-watch.component.ts
index d3e16c4cf..4a67d456e 100644
--- a/client/src/app/videos/+video-watch/video-watch.component.ts
+++ b/client/src/app/videos/+video-watch/video-watch.component.ts
@@ -25,6 +25,7 @@ import { getVideojsOptions, loadLocale, addContextMenu } from '../../../assets/p
25import { ServerService } from '@app/core' 25import { ServerService } from '@app/core'
26import { I18n } from '@ngx-translate/i18n-polyfill' 26import { I18n } from '@ngx-translate/i18n-polyfill'
27import { environment } from '../../../environments/environment' 27import { environment } from '../../../environments/environment'
28import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
28 29
29@Component({ 30@Component({
30 selector: 'my-video-watch', 31 selector: 'my-video-watch',
@@ -377,7 +378,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
377 }) 378 })
378 379
379 if (this.videojsLocaleLoaded === false) { 380 if (this.videojsLocaleLoaded === false) {
380 await loadLocale(environment.apiUrl, videojs, environment.production === true ? this.localeId : 'fr') 381 await loadLocale(environment.apiUrl, videojs, isOnDevLocale() ? getDevLocale() : this.localeId)
381 this.videojsLocaleLoaded = true 382 this.videojsLocaleLoaded = true
382 } 383 }
383 384
diff --git a/client/src/assets/player/peertube-player.ts b/client/src/assets/player/peertube-player.ts
index b604097fa..9e37b75d2 100644
--- a/client/src/assets/player/peertube-player.ts
+++ b/client/src/assets/player/peertube-player.ts
@@ -12,7 +12,7 @@ import './peertube-videojs-plugin'
12import './peertube-load-progress-bar' 12import './peertube-load-progress-bar'
13import { videojsUntyped } from './peertube-videojs-typings' 13import { videojsUntyped } from './peertube-videojs-typings'
14import { buildVideoEmbed, buildVideoLink, copyToClipboard } from './utils' 14import { buildVideoEmbed, buildVideoLink, copyToClipboard } from './utils'
15import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n' 15import { getCompleteLocale, is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
16 16
17// Change 'Playback Rate' to 'Speed' (smaller for our settings menu) 17// Change 'Playback Rate' to 'Speed' (smaller for our settings menu)
18videojsUntyped.getComponent('PlaybackRateMenuButton').prototype.controlText_ = 'Speed' 18videojsUntyped.getComponent('PlaybackRateMenuButton').prototype.controlText_ = 'Speed'
@@ -141,11 +141,13 @@ function addContextMenu (player: any, videoEmbedUrl: string) {
141} 141}
142 142
143function loadLocale (serverUrl: string, videojs: any, locale: string) { 143function loadLocale (serverUrl: string, videojs: any, locale: string) {
144 if (!is18nLocale(locale) || isDefaultLocale(locale)) return undefined 144 const completeLocale = getCompleteLocale(locale)
145 145
146 return fetch(serverUrl + '/client/locales/' + locale + '/player.json') 146 if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return Promise.resolve(undefined)
147
148 return fetch(serverUrl + '/client/locales/' + completeLocale + '/player.json')
147 .then(res => res.json()) 149 .then(res => res.json())
148 .then(json => videojs.addLanguage(locale, json)) 150 .then(json => videojs.addLanguage(completeLocale, json))
149} 151}
150 152
151export { 153export {
diff --git a/client/src/main.ts b/client/src/main.ts
index 19f45a3e3..061be17de 100644
--- a/client/src/main.ts
+++ b/client/src/main.ts
@@ -5,14 +5,17 @@ import { AppModule } from './app/app.module'
5import { environment } from './environments/environment' 5import { environment } from './environments/environment'
6 6
7import { hmrBootstrap } from './hmr' 7import { hmrBootstrap } from './hmr'
8import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
8 9
9let providers = [] 10let providers = []
10if (environment.production) { 11if (environment.production) {
11 enableProdMode() 12 enableProdMode()
12} 13}
13 14
14if (environment.production === false && window.location.search === '?lang=fr') { 15// Template translation, should be in the bootstrap step
15 const translations = require(`raw-loader!./locale/target/angular_fr.xml`) 16if (isOnDevLocale()) {
17 const locale = getDevLocale()
18 const translations = require(`raw-loader!./locale/target/angular_${locale}.xml`)
16 19
17 providers = [ 20 providers = [
18 { provide: TRANSLATIONS, useValue: translations }, 21 { provide: TRANSLATIONS, useValue: translations },