forked from onecx/onecx-portal-ui-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: timeago now uses userService lang$ to change configuration
- Loading branch information
Showing
4 changed files
with
46 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
libs/angular-accelerator/src/lib/utils/ocxtimeagointl.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { filter, map } from 'rxjs' | ||
import { IL10nsStrings, TimeagoIntl } from 'ngx-timeago' | ||
import { strings as englishStrings } from 'ngx-timeago/language-strings/en' | ||
import { strings as germanStrings } from 'ngx-timeago/language-strings/de' | ||
import { UserService } from '@onecx/angular-integration-interface' | ||
|
||
export class OcxTimeagoIntl extends TimeagoIntl { | ||
private LANG_TO_STRINGS: { [key: string]: IL10nsStrings } = { | ||
en: englishStrings, | ||
de: germanStrings, | ||
} | ||
private DEFAULT_LANG = 'en' | ||
|
||
constructor(protected userService: UserService) { | ||
super() | ||
this.strings = englishStrings | ||
userService.lang$ | ||
.pipe( | ||
filter((v) => v !== undefined), | ||
map((lang) => { | ||
return this.getBestMatchLanguage(lang as string) | ||
}) | ||
) | ||
.subscribe((lang) => { | ||
this.strings = this.LANG_TO_STRINGS[lang] | ||
this.changes.next() | ||
}) | ||
} | ||
|
||
getBestMatchLanguage(lang: string) { | ||
if (Object.keys(this.LANG_TO_STRINGS).includes(lang)) { | ||
return lang | ||
} else { | ||
console.log(`${lang} is not supported. Using ${this.DEFAULT_LANG} as a fallback.`) | ||
} | ||
return this.DEFAULT_LANG | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters