Skip to content

Commit

Permalink
chore: support array
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Nov 29, 2023
1 parent e431c0f commit 9d9f615
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/theme/src/services/i18n/i18n.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ALAIN_I18N_TOKEN } from './i18n';
export class I18nPipe implements PipeTransform {
private readonly i18n = inject(ALAIN_I18N_TOKEN);

transform(key: string, params?: unknown): string {
transform(key: string, params?: unknown | unknown[]): string {
return this.i18n.fanyi(key, params);
}
}
33 changes: 27 additions & 6 deletions packages/theme/src/services/i18n/i18n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,40 @@ describe('theme: i18n', () => {
srv = fixture.debugElement.injector.get(ALAIN_I18N_TOKEN);
srv.use('en', {
simple: 'a',
param: 'a-{{value}}'
param: 'a-{{value}}',
paramArr: 'a-{0},{ 1 }'
});
fixture.detectChanges();
});
it('should working', () => {
check('a');
});

it('should be param', () => {
fixture.componentInstance.key = 'param';
fixture.componentInstance.params = { value: '1' };
fixture.detectChanges();
check('a-1', 'param');
describe('#param', () => {
it('with object', () => {
fixture.componentInstance.key = 'param';
fixture.componentInstance.params = { value: '1' };
fixture.detectChanges();
check('a-1', 'param');
});
it('with base type', () => {
fixture.componentInstance.key = 'paramArr';
fixture.componentInstance.params = 'A';
fixture.detectChanges();
check('a-A,{ 1 }', 'param');
});
it('with base type', () => {
fixture.componentInstance.key = 'paramArr';
fixture.componentInstance.params = 100;
fixture.detectChanges();
check('a-100,{ 1 }', 'param');
});
it('with array', () => {
fixture.componentInstance.key = 'paramArr';
fixture.componentInstance.params = [1, 2];
fixture.detectChanges();
check('a-1,2', 'param');
});
});

it('should be return path when is invalid', () => {
Expand Down
19 changes: 13 additions & 6 deletions packages/theme/src/services/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface AlainI18NService {
* @param params 模板所需要的参数对象
* @param isSafe 是否返回安全字符,自动调用 `bypassSecurityTrustHtml`; Should be removed, If you need SafeHtml support, please use `| html` pipe instead.
*/
fanyi(path: string, params?: unknown): string;
fanyi(path: string, params?: unknown | unknown[]): string;
}

export const ALAIN_I18N_TOKEN = new InjectionToken<AlainI18NService>('alainI18nToken', {
Expand Down Expand Up @@ -122,20 +122,27 @@ export abstract class AlainI18nBaseService implements AlainI18NService {

abstract getLangs(): NzSafeAny[];

fanyi(path: string, params?: Record<string, unknown>): string {
fanyi(path: string, params?: unknown | unknown[]): string {
let content = this._data[path] || '';
if (!content) return path;

if (params) {
if (!params) return content;

if (typeof params === 'object') {
const interpolation = this.cog.interpolation!!;
Object.keys(params).forEach(
const objParams = params as Record<string, unknown>;
Object.keys(objParams).forEach(
key =>
(content = content.replace(
new RegExp(`${interpolation[0]}\s?${key}\s?${interpolation[1]}`, 'g'),
`${params[key]}`
new RegExp(`${interpolation[0]}\\s?${key}\\s?${interpolation[1]}`, 'g'),
`${objParams[key]}`
))
);
}

(Array.isArray(params) ? params : [params]).forEach(
(item, index) => (content = content.replace(new RegExp(`\\{\\s?${index}\\s?\\}`, 'g'), `${item}`))
);
return content;
}
}
Expand Down

0 comments on commit 9d9f615

Please sign in to comment.