@@ -40,24 +40,68 @@ export class NgxI18nRootService {
4040 * @param language - The provided language
4141 */
4242 public setCurrentLanguage ( language : string ) : void {
43- // Iben: If a language is set that's not part of the available languages, we return a warn
43+ // Iben: get the new language
44+ const newLanguage = this . getNewLanguage ( language ) ;
45+
46+ // Iben: Save the current language to the localStorage when we're in the browser
47+ if ( isPlatformBrowser ( this . platformId ) ) {
48+ localStorage . setItem ( 'ngx-i18n-language' , newLanguage ) ;
49+ }
50+
51+ // Iben: Update the subject
52+ this . currentLanguageSubject . next ( newLanguage ) ;
53+ }
54+
55+ /**
56+ * Sets the initial language of the application when no language is set yet.
57+ *
58+ * If a previous language was set in the local storage, said language is used. If not, the default language gets used.
59+ */
60+ public initializeLanguage ( ) : void {
61+ // Iben: If the current language already exists, we early exit
62+ if ( this . currentLanguage ) {
63+ return ;
64+ }
65+
66+ // Iben: If the current language does not exist, we check if it exists in the local storage, if not, we use the default config
67+ let language = this . configuration . defaultLanguage ;
68+
69+ if ( isPlatformBrowser ( this . platformId ) ) {
70+ language =
71+ localStorage . getItem ( 'ngx-i18n-language' ) || this . configuration . defaultLanguage ;
72+ }
73+
74+ // Iben: We set the new language
75+ this . setCurrentLanguage ( language ) ;
76+ }
77+
78+ /**
79+ * Checks if the newly proposed language can be set, if not we return either the current language or the default language
80+ *
81+ * @param {string } language - The newly proposed language
82+ */
83+ private getNewLanguage ( language : string ) : string {
84+ // Iben: Save the currently being set language
85+ let newLanguage = language ;
86+
87+ // Iben: Check if the new language is part of the available languages
4488 if ( ! this . configuration . availableLanguages . includes ( language ) ) {
89+ // Iben: If a language is set that's not part of the available languages, we return a warn
4590 console . warn (
4691 `NgxI18n: A language, ${ language } , was attempted to be set that was not part of the available languages (${ this . configuration . availableLanguages . join (
4792 ', '
4893 ) } )`
4994 ) ;
5095
51- // Iben: Early exit
52- return ;
53- }
96+ // Iben: If there is already a language set, we early exit and keep the remaining language
97+ if ( this . currentLanguage ) {
98+ return this . currentLanguage ;
99+ }
54100
55- // Iben: Save the current language to the localStorage when we're in the browser
56- if ( isPlatformBrowser ( this . platformId ) ) {
57- localStorage . setItem ( 'ngx-language' , language ) ;
101+ // Iben: If no language exists, we use the default language
102+ newLanguage = this . configuration . defaultLanguage ;
58103 }
59104
60- // Iben: Update the subject
61- this . currentLanguageSubject . next ( language ) ;
105+ return newLanguage ;
62106 }
63107}
0 commit comments