Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(i18n): corrigido conflito de literais entre app e lib no PO UI #2347

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
31 changes: 24 additions & 7 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,12 +182,27 @@ export class PoI18nModule {
}
}

export function initializeLanguageDefault(config: PoI18nConfig, languageService: PoLanguageService) {
// eslint-disable-next-line sonarjs/prefer-immediate-return
const setDefaultLanguage = () => {
if (config.default.language) {
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`

return () => {
if (config?.default.language) {
languageService.setLanguageDefault(config.default.language);
}
};
return setDefaultLanguage;
}
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);
}
Loading