Skip to content

Commit

Permalink
fix(i18n): corrigido conflito de literais entre app e lib no PO UI
Browse files Browse the repository at this point in the history
- Resolvido problema onde os literais da lib não eram carregados
  corretamente quando a lib era adicionada em um app que também
  utiliza i18n, devido ao comportamento de sobrescrição da configuração
  do app principal.
- Ajustado o comportamento do carregamento dos literais para garantir
  que cada configuração do i18n (do app e da lib) seja aplicada separadamente,
  evitando conflitos entre as configurações de ambos.

Fixes DTHFUI-10440
  • Loading branch information
renanarosario committed Jan 8, 2025
1 parent 0b12f25 commit 1dbaa55
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"colors": "1.4.0",
"core-js": "3.33.3",
"custom-idle-queue": "3.0.1",
"deepmerge": "^4.3.1",
"eslint-plugin-sonarjs": "^0.23.0",
"gulp-clean": "^0.4.0",
"gulp-run": "^1.7.1",
Expand Down
2 changes: 1 addition & 1 deletion projects/ui/ng-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"lib": {
"entryFile": "./src/public-api.ts"
},
"allowedNonPeerDependencies": ["@angular/cdk", "@po-ui/style", "@po-ui/ng-schematics"]
"allowedNonPeerDependencies": ["@angular/cdk", "@po-ui/style", "@po-ui/ng-schematics", "deepmerge"]
}
3 changes: 2 additions & 1 deletion projects/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@angular-devkit/schematics": "^18",
"@po-ui/style": "0.0.0-PLACEHOLDER",
"rxjs": "~7.8.1",
"zone.js": "~0.14.4"
"zone.js": "~0.14.4",
"deepmerge": "^4.3.1"
}
}
1 change: 1 addition & 0 deletions projects/ui/src/lib/services/po-i18n/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './interfaces/po-i18n-config.interface';
export * from './interfaces/po-i18n-config-context.interface';
export * from './interfaces/po-i18n-config-default.interface';
export * from './interfaces/po-i18n-literals.interface';
export * from './po-i18n.pipe';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @description
*
* <a id="poI18nConfigContext"></a>
*
* Interface para a configuração dos contextos do módulo `PoI18nModule`.
*
* @usedBy PoI18nModule
*/
export interface PoI18nConfigContext {
[name: string]: { [language: string]: { [literal: string]: string } } | { url: string };
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PoI18nConfigDefault } from './po-i18n-config-default.interface';
import { PoI18nConfigContext } from './po-i18n-config-context.interface';

/**
* @description
Expand Down Expand Up @@ -76,5 +77,5 @@ export interface PoI18nConfig {
* ```
* > Caso a constante contenha alguma literal que o serviço não possua será utilizado a literal da constante.
*/
contexts: object;
contexts: PoI18nConfigContext;
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class PoI18nBaseService {
const context = options['context'] ? options['context'] : this.contextDefault;
const literals: Array<string> = options['literals'] ? options['literals'] : [];

return new Observable(observer => {
return new Observable<any>(observer => {
if (this.servicesContext[context]) {
// Faz o processo de busca de um contexto que contém serviço
this.getLiteralsFromContextService(language, context, literals, observer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import { InjectionToken } from '@angular/core';

import { PoI18nConfig } from './interfaces/po-i18n-config.interface';

export const I18N_CONFIG = new InjectionToken<PoI18nConfig>('I18N_CONFIG');
export const I18N_CONFIG = new InjectionToken<Array<PoI18nConfig>>('I18N_CONFIG');
27 changes: 22 additions & 5 deletions projects/ui/src/lib/services/po-i18n/po-i18n.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APP_INITIALIZER, ModuleWithProviders, NgModule } from '@angular/core';
import { APP_INITIALIZER, ModuleWithProviders, NgModule, Provider } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { PoLanguageService } from './../po-language/po-language.service';
Expand Down Expand Up @@ -162,8 +162,10 @@ export class PoI18nModule {
providers: [
{
provide: I18N_CONFIG,
useValue: config
useValue: config,
multi: true
},
provideI18nConfig(config),
{
provide: APP_INITIALIZER,
useFactory: initializeLanguageDefault,
Expand All @@ -180,10 +182,25 @@ export class PoI18nModule {
}
}

export function initializeLanguageDefault(config: PoI18nConfig, languageService: PoLanguageService) {
// eslint-disable-next-line sonarjs/prefer-immediate-return
export function provideI18nConfig(config: PoI18nConfig): Array<Provider> {
return [
{
provide: I18N_CONFIG,
useValue: config,
multi: true
},
{
provide: PoI18nService,
useFactory: returnPoI18nService,
deps: [I18N_CONFIG, HttpClient, PoLanguageService]
}
];
}

export function initializeLanguageDefault(configs: Array<PoI18nConfig>, languageService: PoLanguageService) {
const config = configs.find(c => c.default); // Busca a configuração com `default`
const setDefaultLanguage = () => {

Check failure on line 202 in projects/ui/src/lib/services/po-i18n/po-i18n.module.ts

View workflow job for this annotation

GitHub Actions / lint

Immediately return this expression instead of assigning it to the temporary variable "setDefaultLanguage"
if (config.default.language) {
if (config?.default.language) {
languageService.setLanguageDefault(config.default.language);
}
};
Expand Down
16 changes: 14 additions & 2 deletions projects/ui/src/lib/services/po-i18n/po-i18n.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { all as deepmergeAll } from 'deepmerge';

import { PoLanguageService } from './../po-language/po-language.service';

Expand All @@ -14,6 +15,17 @@ import { PoI18nConfig } from './interfaces/po-i18n-config.interface';
export class PoI18nService extends PoI18nBaseService {}

// Função usada para retornar instância para o módulo po-i18n.module
export function returnPoI18nService(config: PoI18nConfig, http: HttpClient, languageService: PoLanguageService) {
return new PoI18nService(config, http, languageService);
export function returnPoI18nService(
configs: Array<PoI18nConfig>,
http: HttpClient,
languageService: PoLanguageService
) {
const validatedConfigs = configs.map(config => ({
...config,
contexts: config.contexts,
default: config.default
}));
const mergedConfig = deepmergeAll<PoI18nConfig>(validatedConfigs);

return new PoI18nService(mergedConfig, http, languageService);
}

0 comments on commit 1dbaa55

Please sign in to comment.