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

Use $i18n in Vuex store using Typescript #1146

Closed
florian-lefebvre opened this issue Apr 13, 2021 · 5 comments · Fixed by #1147
Closed

Use $i18n in Vuex store using Typescript #1146

florian-lefebvre opened this issue Apr 13, 2021 · 5 comments · Fixed by #1147

Comments

@florian-lefebvre
Copy link

I'm migrating my Nuxt app to Typescript.
In store/api.js, I have the following:

export const actions = {
// ...
getTranslation({}, { set }) {
    return set.translations.find(
      t => t.code == this.$i18n.localeProperties.code
    );
  },
}

But now, in api.ts, I can't do:

import { Module, VuexModule, Mutation, Action } from "vuex-module-decorators";
import { Translation, LocaleSet } from "@/utils/interfaces";

@Module({
  name: "api",
  stateFactory: true,
  namespaced: true
})
export default class Api extends VuexModule {
  //...
  @Action
  getTranslation(set: any): Translation {
    return set.translations.find(
      (t: Translation) => t.code == this.$i18n.localeProperties.code // this.context.$i18n also doesn't work
    );
  }
}

In the docs, there is a guide to access the modules.
The issue is that I didn't manage to import any type from the module like:

// utils/store.js
import { SomeI18nType } from "nuxt-i18n";
let $i18n: SomeI18nType;

export function initializeI18n(i18nInstance: SomeI18nType): void {
  $i18n = i18nInstance;
}

export { $i18n };

How can I do ?

@rchl
Copy link
Collaborator

rchl commented Apr 13, 2021

Gonna expose NuxtI18nInstance in #1147 so you'll be able able to type the $i18n variable properly.

(In theory, you could have done that already by pulling the type from Context['i18n'], for example, but it makes sense to expose it anyway.)

@florian-lefebvre
Copy link
Author

Thanks !

@florian-lefebvre
Copy link
Author

When will it be available ?

@rchl
Copy link
Collaborator

rchl commented Apr 13, 2021

Released https://github.com/nuxt-community/i18n-module/releases/tag/v6.25.0

@atlansien
Copy link

atlansien commented Apr 6, 2023

I can't find NuxtI18nInstance in v8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants