From 7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 6 Jun 2018 16:46:42 +0200 Subject: Add server localization --- client/src/app/shared/video/video-details.model.ts | 4 +- client/src/app/shared/video/video.model.ts | 8 +++- client/src/app/shared/video/video.service.ts | 56 +++++++++++++--------- 3 files changed, 43 insertions(+), 25 deletions(-) (limited to 'client/src/app/shared/video') diff --git a/client/src/app/shared/video/video-details.model.ts b/client/src/app/shared/video/video-details.model.ts index 5fc55fca6..19c350ab3 100644 --- a/client/src/app/shared/video/video-details.model.ts +++ b/client/src/app/shared/video/video-details.model.ts @@ -15,8 +15,8 @@ export class VideoDetails extends Video implements VideoDetailsServerModel { likesPercent: number dislikesPercent: number - constructor (hash: VideoDetailsServerModel) { - super(hash) + constructor (hash: VideoDetailsServerModel, translations = {}) { + super(hash, translations) this.descriptionPath = hash.descriptionPath this.files = hash.files diff --git a/client/src/app/shared/video/video.model.ts b/client/src/app/shared/video/video.model.ts index 48d562f9c..d37dc2c3e 100644 --- a/client/src/app/shared/video/video.model.ts +++ b/client/src/app/shared/video/video.model.ts @@ -5,6 +5,7 @@ import { VideoConstant } from '../../../../../shared/models/videos/video.model' import { getAbsoluteAPIUrl } from '../misc/utils' import { ServerConfig } from '../../../../../shared/models' import { Actor } from '@app/shared/actor/actor.model' +import { peertubeTranslate } from '@app/shared/i18n/i18n-utils' export class Video implements VideoServerModel { by: string @@ -68,7 +69,7 @@ export class Video implements VideoServerModel { minutes.toString() + ':' + secondsPadding + seconds.toString() } - constructor (hash: VideoServerModel) { + constructor (hash: VideoServerModel, translations = {}) { const absoluteAPIUrl = getAbsoluteAPIUrl() this.createdAt = new Date(hash.createdAt.toString()) @@ -98,6 +99,11 @@ export class Video implements VideoServerModel { this.by = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host) this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account) + + this.category.label = peertubeTranslate(this.category.label, translations) + this.licence.label = peertubeTranslate(this.licence.label, translations) + this.language.label = peertubeTranslate(this.language.label, translations) + this.privacy.label = peertubeTranslate(this.privacy.label, translations) } isVideoNSFWForUser (user: User, serverConfig: ServerConfig) { diff --git a/client/src/app/shared/video/video.service.ts b/client/src/app/shared/video/video.service.ts index d1e32faeb..c607b7d6a 100644 --- a/client/src/app/shared/video/video.service.ts +++ b/client/src/app/shared/video/video.service.ts @@ -1,4 +1,4 @@ -import { catchError, map } from 'rxjs/operators' +import { catchError, map, switchMap } from 'rxjs/operators' import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http' import { Injectable } from '@angular/core' import { Observable } from 'rxjs' @@ -24,6 +24,7 @@ import { Account } from '@app/shared/account/account.model' import { AccountService } from '@app/shared/account/account.service' import { VideoChannel } from '../../../../../shared/models/videos' import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' +import { ServerService } from '@app/core' @Injectable() export class VideoService { @@ -33,7 +34,8 @@ export class VideoService { constructor ( private authHttp: HttpClient, private restExtractor: RestExtractor, - private restService: RestService + private restService: RestService, + private serverService: ServerService ) {} getVideoViewUrl (uuid: string) { @@ -41,9 +43,13 @@ export class VideoService { } getVideo (uuid: string): Observable { - return this.authHttp.get(VideoService.BASE_VIDEO_URL + uuid) + return this.serverService.localeObservable .pipe( - map(videoHash => new VideoDetails(videoHash)), + switchMap(translations => { + return this.authHttp.get(VideoService.BASE_VIDEO_URL + uuid) + .pipe(map(videoHash => ({ videoHash, translations }))) + }), + map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)), catchError(res => this.restExtractor.handleError(res)) ) } @@ -102,9 +108,10 @@ export class VideoService { let params = new HttpParams() params = this.restService.addRestGetParams(params, pagination, sort) - return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params }) + return this.authHttp + .get>(UserService.BASE_USERS_URL + '/me/videos', { params }) .pipe( - map(this.extractVideos), + switchMap(res => this.extractVideos(res)), catchError(res => this.restExtractor.handleError(res)) ) } @@ -120,9 +127,9 @@ export class VideoService { params = this.restService.addRestGetParams(params, pagination, sort) return this.authHttp - .get(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params }) + .get>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params }) .pipe( - map(this.extractVideos), + switchMap(res => this.extractVideos(res)), catchError(res => this.restExtractor.handleError(res)) ) } @@ -138,9 +145,9 @@ export class VideoService { params = this.restService.addRestGetParams(params, pagination, sort) return this.authHttp - .get(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params }) + .get>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params }) .pipe( - map(this.extractVideos), + switchMap(res => this.extractVideos(res)), catchError(res => this.restExtractor.handleError(res)) ) } @@ -160,9 +167,9 @@ export class VideoService { } return this.authHttp - .get(VideoService.BASE_VIDEO_URL, { params }) + .get>(VideoService.BASE_VIDEO_URL, { params }) .pipe( - map(this.extractVideos), + switchMap(res => this.extractVideos(res)), catchError(res => this.restExtractor.handleError(res)) ) } @@ -230,7 +237,7 @@ export class VideoService { return this.authHttp .get>(url, { params }) .pipe( - map(this.extractVideos), + switchMap(res => this.extractVideos(res)), catchError(res => this.restExtractor.handleError(res)) ) } @@ -287,14 +294,19 @@ export class VideoService { } private extractVideos (result: ResultList) { - const videosJson = result.data - const totalVideos = result.total - const videos = [] - - for (const videoJson of videosJson) { - videos.push(new Video(videoJson)) - } - - return { videos, totalVideos } + return this.serverService.localeObservable + .pipe( + map(translations => { + const videosJson = result.data + const totalVideos = result.total + const videos: Video[] = [] + + for (const videoJson of videosJson) { + videos.push(new Video(videoJson, translations)) + } + + return { videos, totalVideos } + }) + ) } } -- cgit v1.2.3