aurelia / i18n

A plugin that provides i18n support.
MIT License
93 stars 70 forks source link

Can't return arrays in tr() anymore? #331

Closed blarsern closed 1 year ago

blarsern commented 3 years ago

I'm submitting a bug report

Please tell us about your environment:

Current behavior:

dist/aurelia-i18n.d.ts changed 27th of mars 2019:

Changed from: tr(key: string | string[], options?: i18next.TranslationOptions): any;

To: tr(key: string | string[], options?: i18next.TOptions): string;

But in 18n.ts it's still any: public tr(key: string | string[], options?: i18next.TOptions) { let fullOptions = this.globalVars;

if (options !== undefined) {
  fullOptions = Object.assign(Object.assign({}, this.globalVars), options);
}

return this.i18next.t(key, fullOptions); }

So after upgrade from beta 5 this doesn't compile anymore: let dayNames: Array = this.i18n.tr('weekdays', { returnObjects: true });

Expected/desired behavior: Returning arrays should work ?

zewa666 commented 3 years ago

Ouch, yep that was a mistake I forgot about the returnObject parameter. So it really should be unknown or any right?

blarsern commented 3 years ago

Probably not used a lot ;)

Not sure what's best of unknown or any..

zewa666 commented 3 years ago

ok so the proper result seems to be TResult which can be string | object | Array<string | object> | undefined.

Please give the fixed version in the PR a try. Clone the tr-result branch, run npm run build and then install in your project via npm install ../path/to/your/local/clone/root.

If all is good we can merge and create another patch release.

blarsern commented 3 years ago

Well tested it.

Now this simple code fails: return { name: this.i18n.tr('enums.myEnum.ngh') };

TS2322: Type 'TrResult' is not assignable to type 'string'. Type 'object' is not assignable to type 'string'.

To fix this, i have to add toString: return { name: this.i18n.tr('enums.myEnum.ngh').toString() };

var a:string = this.i18n.tr('enums.myEnum.ngh'); //Not working var a:string = this.i18n.tr('enums.myEnum.ngh').toString(); //Working

So basically this will require a lot of toString() everywhere.

Too much work to get it to compile, so didn't test arrays :)

zewa666 commented 3 years ago

Thx for the Test and the Testcase, I'll fix that when at the PC.

zewa666 commented 3 years ago

ok lets give the next version a try, I've pushed the changes @blarsern

blarsern commented 3 years ago

Seems to be working perfectly @zewa666

Thanks!