aurelia / i18n

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

Namespace 'i18next' has no exported member 'TranslationFunction' #295

Open developer-pta opened 5 years ago

developer-pta commented 5 years ago

I'm submitting a bug report

Please tell us about your environment:

Current behavior: On 'au build' it throws errors: error TS2694: Namespace 'i18next' has no exported member 'TranslationFunction' error TS2694: Namespace 'i18next' has no exported member 'TranslationOptions'

Expected/desired behavior:

developer-pta commented 5 years ago

Ok, this errors are in module /node_modules/aurelia-i18n/dist/aurelia-i18n.d.ts and it seems to be related to: https://github.com/i18next/i18next/pull/1180 where they introduced a BREAKING CHANGE i v.14 of i18next.

the breaking change is:

TranslateFunction -> TFunction TranslateOptions -> TOptions WithT.t - removed definition and extracted to TFunction exists - stopped (mis)using TranslateFunction and defined separately reorder the TFunction args to make easier for typical use (result first) default TFunction to return string by default. For arrays and objects, parameterize the call e.g. t('foo')

So by renameing those calls in aurelia-i18n.d.ts, should work fine. Will try then report.

bigopon commented 5 years ago

@ProtechAutomation Cool, thanks for the finding. The definition was built with v11 or 12 I think, and newer version of i18next will probably requires some updates from i18next plugin. It should not be difficult to do so though. Would be nice if you could create a PR to fix this

developer-pta commented 5 years ago

@ProtechAutomation Cool, thanks for the finding. The definition was built with v11 or 12 I think, and newer version of i18next will probably requires some updates from i18next plugin. It should not be difficult to do so though. Would be nice if you could create a PR to fix this

Will try. I have never done this (PR). And yes, the build is passing if i make those changes.

bigopon commented 5 years ago

@ProtechAutomation You can follow the instruction for development in README.md, if you hit any issues, please post it here ❤️

developer-pta commented 5 years ago

Hmm... It seem that those were not the only changes they made. This was also changed:

functions used to return a callback (eg. init, changeLanguage, loadNamespaces) now also return a Promise - while this enables easier handling using async await this also means eg. i18next.init does not return this anylonger and therefore you can't chain calls like i18next.init().on() anylonger.

So now i get new problems in:

public async setup(options?: AureliaEnhancedOptions & i18next.InitOptions) {
    const defaultOptions : AureliaEnhancedOptions & i18next.InitOptions = {
      skipTranslationOnMissingKey: false,
      //compatibilityAPI: "v1",
      compatibilityJSON: "v1",
      lng: "en",
      attributes: ["t", "i18n"],
      fallbackLng: "en",
      debug: false
    };
    this.i18nextDeferred = new Promise((resolve, reject) => {
      this.i18next = this.i18next.init(options || defaultOptions, (err) => {
        if (err) {
          reject(err);
        }
        // make sure attributes is an array in case a string was provided
        if (this.i18next.options.attributes instanceof String) {
          this.i18next.options.attributes = [this.i18next.options.attributes as any as string];
        }
        resolve(this.i18next);
      });
    });
    return this.i18nextDeferred;
}

Where first I had to make the type of defaultOptions explict, and remove 'compatibilityAPI: "v1"'. Now vscode is complaining of the i18next.init method not returning the correct type. The init method now returns: Promise while aurelia.i18n expects: public i18next: AureliaEnhancedI18Next;

developer-pta commented 5 years ago

Ok, I think I found some way to fix this. Because the 'Init' function no longer return the expected i18Next.i18n type, it can't be use to create new instances. so i find another function that does this and returns the i18Next.i18n type : 'createInstance()'

I replaced the init line with this.i18next.createInstance() like this:

this.i18next = this.i18next.createInstance(options || defaultOptions, (err) => {
    if (err) { 
        reject(err);
    }
     // make sure attributes is an array in case a string was provided
    if (this.i18next.options.attributes instanceof String) { 
        this.i18next.options.attributes = [this.i18next.options.attributes as any as string]; 
    } 
    resolve(this.i18next); 
});

So far VS code does not show any errors in code. Will try to build and run tests to see if this works.

developer-pta commented 5 years ago

Ok,

npm run build ...ok

npm run test ...ok

I created a pull request for it: fix-for-295

mredl commented 5 years ago

Problem still exists. I installed aurelia-i18n yesterday, got 3.0.0-beta.5, and during build i get following 3 errors: c:/..../node_modules/aurelia-i18n/dist/aurelia-i18n.d.ts(31,45): error TS2694: Namespace 'i18next' has no exported member 'TranslationFunction'. c:/..../node_modules/aurelia-i18n/dist/aurelia-i18n.d.ts(36,47): error TS2694: Namespace 'i18next' has no exported member 'TranslationOptions'. c:/..../node_modules/aurelia-i18n/dist/aurelia-i18n.d.ts(173,39): error TS2694: Namespace 'i18next' has no exported member 'TranslationOptions'.

Edit: aurelia-i18n.d.ts still contained old types in this lines. i have changed them to the types of the PR and the build errors have gone away.

EisenbergEffect commented 5 years ago

@zewa666 Any ideas?

zewa666 commented 5 years ago

@mredl thats weird, can you check on a fresh new project what gets installed and whether the old types are present in the same d.ts file?

mredl commented 5 years ago

i did! that was a fresh new project, created with cli on the same day. the first plugin i installed was aurelia-18n. then i got this ts errors which i have fixed. but the app still hangs during loading of aurelia-18n. if i uncomment the loading in main.ts the app boots.

Edit: i have tried it now on a different workstation, where no aurelia app has been coded before, there it works without troubles. it looks like npm has pulled some modules from local cache, that an older version of aurelia-i18n or dependency got installed

zewa666 commented 5 years ago

@mredl oh wow that was super dumb mistake from our side :( We've forgot to release the latest version on npm and I was merely testing it from a local branch. Sorry for that. @EisenbergEffect can you create another patch release?

EisenbergEffect commented 5 years ago

I'll get a release out tonight 😄

EisenbergEffect commented 5 years ago

@zewa666 I don't think I can release this yet. There seems to still be an issue. In the generated d.ts file for the library, there are two imports for i18next, with two different syntaxes. This makes the d.ts invalid. It also leads me to be believe that one of the imports, which isn't correct, is still used somewhere in the codebase. Can you investigate? You can see the issue if you build master and look at the generated d.ts. Let me know.