1
1
import { Inject , Injectable } from '@angular/core' ;
2
2
import { BehaviorSubject , combineLatest , from , Observable , Subject } from 'rxjs' ;
3
- import { catchError , map , retry , shareReplay , tap } from 'rxjs/operators' ;
3
+ import { catchError , map , retry , shareReplay , tap , distinctUntilChanged } from 'rxjs/operators' ;
4
4
import { TRANSLOCO_LOADER , TranslocoLoader } from './transloco.loader' ;
5
5
import { TRANSLOCO_TRANSPILER , TranslocoTranspiler } from './transloco.transpiler' ;
6
6
import { HashMap , Translation , TranslationCb , TranslocoEvents } from './types' ;
7
7
import {
8
- dashCaseToCamelCase ,
8
+ toCamelCase ,
9
9
getLangFromScope ,
10
10
getScopeFromLang ,
11
- getValue ,
11
+ getValue , isEmpty ,
12
12
isFunction ,
13
13
mergeDeep ,
14
14
setValue ,
15
15
size ,
16
- isEmpty
17
16
} from './helpers' ;
18
17
import { defaultConfig , TRANSLOCO_CONFIG , TranslocoConfig } from './transloco.config' ;
19
18
import { TRANSLOCO_MISSING_HANDLER , TranslocoMissingHandler } from './transloco-missing-handler' ;
@@ -61,13 +60,13 @@ export class TranslocoService {
61
60
this . mergedConfig = { ...defaultConfig , ...this . userConfig } ;
62
61
this . setDefaultLang ( this . mergedConfig . defaultLang ) ;
63
62
this . lang = new BehaviorSubject < string > ( this . getDefaultLang ( ) ) ;
64
- this . langChanges$ = this . lang . asObservable ( ) ;
63
+ this . langChanges$ = this . lang . asObservable ( ) . pipe ( distinctUntilChanged ( ) ) ;
65
64
66
65
/**
67
66
* When we have a failure, we want to define the next language that succeeded as the active
68
67
*/
69
68
this . events$ . subscribe ( e => {
70
- if ( e . type === 'translationLoadSuccess' && e . wasFailure ) {
69
+ if ( e . type === 'translationLoadSuccess' && e . wasFailure ) {
71
70
// Handle scoped lang
72
71
const lang = getLangFromScope ( e . payload . lang ) ;
73
72
this . setActiveLang ( lang ) ;
@@ -101,7 +100,7 @@ export class TranslocoService {
101
100
}
102
101
103
102
load ( lang : string , options ?: { fallbackLangs : string [ ] | null } ) : Observable < Translation > {
104
- if ( this . cache . has ( lang ) === false ) {
103
+ if ( this . cache . has ( lang ) === false ) {
105
104
const mergedOptions = { ...{ fallbackLangs : null } , ...( options || { } ) } ;
106
105
107
106
const load$ = from ( this . loader . getTranslation ( lang ) ) . pipe (
@@ -140,22 +139,22 @@ export class TranslocoService {
140
139
params : HashMap = { } ,
141
140
lang ?: string
142
141
) : string | string [ ] {
143
- if ( Array . isArray ( key ) ) {
142
+ if ( Array . isArray ( key ) ) {
144
143
return key . map ( k => this . translate ( k , params , lang ) ) ;
145
144
}
146
145
147
- if ( ! key ) {
146
+ if ( ! key ) {
148
147
return this . missingHandler . handle ( key as string , params , this . config ) ;
149
148
}
150
149
151
150
const translation = this . translations . get ( lang || this . getActiveLang ( ) ) ;
152
- if ( ! translation ) {
151
+ if ( ! translation ) {
153
152
return '' ;
154
153
}
155
154
156
155
const value = isFunction ( key ) ? key ( translation as T , params ) : getValue ( translation , key ) ;
157
156
158
- if ( ! value ) {
157
+ if ( ! value ) {
159
158
return this . missingHandler . handle ( key , params , this . config ) ;
160
159
}
161
160
@@ -232,7 +231,7 @@ export class TranslocoService {
232
231
*/
233
232
setTranslationKey ( key : string , value : string , lang = this . getActiveLang ( ) ) {
234
233
const translation = this . getTranslation ( lang ) ;
235
- if ( ! isEmpty ( translation ) ) {
234
+ if ( ! isEmpty ( translation ) ) {
236
235
const withHook = this . interceptor . preSaveTranslationKey ( key , value , lang ) ;
237
236
const newValue = setValue ( translation , key , withHook ) ;
238
237
this . setTranslation ( newValue , lang ) ;
@@ -247,9 +246,10 @@ export class TranslocoService {
247
246
_loadDependencies ( langName : string ) : Observable < Translation | Translation [ ] > {
248
247
const split = langName . split ( '/' ) ;
249
248
const [ lang ] = split . slice ( - 1 ) ;
250
- if ( split . length > 1 && this . isSharedScope && ! size ( this . getTranslation ( lang ) ) ) {
249
+ if ( split . length > 1 && this . isSharedScope && ! size ( this . getTranslation ( lang ) ) ) {
251
250
return combineLatest ( this . load ( lang ) , this . load ( langName ) ) ;
252
251
}
252
+
253
253
return this . load ( langName ) ;
254
254
}
255
255
@@ -259,18 +259,18 @@ export class TranslocoService {
259
259
const { scopeStrategy, scopeMapping = { } } = this . config ;
260
260
const currLang = getLangFromScope ( lang ) ;
261
261
const scope = getScopeFromLang ( lang ) ;
262
- if ( scope && scopeStrategy === 'shared' ) {
262
+ if ( scope && scopeStrategy === 'shared' ) {
263
263
const activeLang = this . getTranslation ( currLang ) ;
264
- const key = dashCaseToCamelCase ( scopeMapping [ scope ] || scope ) ;
264
+ const key = toCamelCase ( scopeMapping [ scope ] || scope ) ;
265
265
const merged = setValue ( activeLang , key , withHook ) ;
266
266
this . translations . set ( currLang , merged ) ;
267
267
}
268
268
}
269
269
270
270
private handleSuccess ( lang : string , translation : Translation ) {
271
271
this . setTranslation ( translation , lang , { emitChange : false } ) ;
272
- if ( this . failedLangs . has ( lang ) === false ) {
273
- if ( ! this . config . prodMode ) {
272
+ if ( this . failedLangs . has ( lang ) === false ) {
273
+ if ( ! this . config . prodMode ) {
274
274
console . log ( `%c 🍻 Translation Load Success: ${ lang } ` , 'background: #fff; color: hotpink;' ) ;
275
275
}
276
276
@@ -298,9 +298,9 @@ export class TranslocoService {
298
298
299
299
const isFallbackLang = nextLang === splitted [ splitted . length - 1 ] ;
300
300
301
- if ( ! nextLang || isFallbackLang ) {
301
+ if ( ! nextLang || isFallbackLang ) {
302
302
let msg = `Unable to load translation and all the fallback languages` ;
303
- if ( splitted . length > 1 ) {
303
+ if ( splitted . length > 1 ) {
304
304
msg += `, did you misspelled the scope name?` ;
305
305
}
306
306
@@ -309,7 +309,7 @@ export class TranslocoService {
309
309
310
310
let resolveLang = nextLang ;
311
311
// if it's scoped lang
312
- if ( splitted . length > 1 ) {
312
+ if ( splitted . length > 1 ) {
313
313
// We need to resolve it to:
314
314
// todos/langNotExists => todos/nextLang
315
315
splitted [ splitted . length - 1 ] = nextLang ;
0 commit comments