<%#
 Copyright 2013-2026 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import { inject, NgModule } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateService, TranslateLoader, MissingTranslationHandler } from '@ngx-translate/core';
import { translatePartialLoader, missingTranslationHandler } from 'app/config/translation.config';
import { StateStorageService } from 'app/core/auth/state-storage.service';
import { provideTranslateHttpLoader } from '@ngx-translate/http-loader';

<%_ if (applicationTypeMicroservice) { _%>

@NgModule({
  imports: [
    TranslateModule.forChild({
      loader: provideTranslateHttpLoader({ prefix: 'services/<%= lowercaseBaseName %>/i18n/', suffix: `.json?_=${I18N_HASH}` }),
      isolate: false,
      extend: true,
    }),
  ],
})
export class LazyTranslationModule {
  private readonly translateService = inject(TranslateService);
  private readonly translateLoader = inject(TranslateLoader);
  private readonly stateStorageService = inject(StateStorageService);

  constructor() {
    const currentLang = this.translateService.getCurrentLang();
    this.translateLoader.getTranslation(currentLang).subscribe(translation => {
      this.translateService.setTranslation(currentLang, translation);
    });
  }
}
<%_ } _%>

@NgModule({
  imports: [
    TranslateModule.forRoot({
      <%_ if (clientBundlerEsbuild) { _%>
      loader: {
        provide: TranslateLoader,
        useFactory: translatePartialLoader,
        deps: [HttpClient],
      },
      <%_ } else { _%>
      loader: provideTranslateHttpLoader({ prefix: './i18n/', suffix: `.json?_=${I18N_HASH}` }),
      <%_ } _%>
      missingTranslationHandler: {
        provide: MissingTranslationHandler,
        useFactory: missingTranslationHandler,
      },
    }),
  ],
})
export class TranslationModule {
  private readonly translateService = inject(TranslateService);
  private readonly stateStorageService = inject(StateStorageService);

  constructor() {
    this.translateService.setFallbackLang('<%= nativeLanguage %>');
    // if the user has changed the language and navigates away from the application and back to it, then use the previously chosen language
    const langKey = this.stateStorageService.getLocale() ?? '<%= nativeLanguage %>';
    this.translateService.use(langKey);
  }
}
