i18n icon indicating copy to clipboard operation
i18n copied to clipboard

feat: add support for composition api

Open rchl opened this issue 3 years ago • 12 comments

These are some changes that I have had lying dormant for a while. Still not finished. Need to expose more APIs (I believe), document and test.

@kazupon any comments? Is there something you have already done in that area and/or would you want things to be done differently?

rchl avatar Oct 27 '21 07:10 rchl

@rchl Thank you for your mention!

Great works! but, sorry, I forgot to tell you 🙇

I previously released vue-i18n-bridge for vue 2. that is back-ported from vue-i18n-next composition API like useI18n, and it work on [email protected].

Documentation is here: https://vue-i18n.intlify.dev/guide/migration/ways.html

If you have any questions, please any comments.

kazupon avatar Oct 28 '21 08:10 kazupon

Yes, but the composition API that you've created for vue-i18n is not enough since this module exposes extra properties and functions that also need to be provided through composition API.

So the question is, should this module create its own composition API like in this PR or maybe expose a wrapper for vue-i18n API that also adds extra properties.

In theory the latter option would be better as it would require less maintenance.

rchl avatar Oct 28 '21 08:10 rchl

So the question is, should this module create its own composition API like in this PR or maybe expose a wrapper for vue-i18n API that also adds extra properties.

In theory the latter option would be better as it would require less maintenance.

Yes, I agree with you. @nuxtjs/i18n itself shouldn't create its own Composition API like useI18n provided by vue-i18n-bridge. The properties and functions provided by @nuxtjs/i18n should be provided by wrapping the Composition API provided by vue-i18n-bridge.

kazupon avatar Oct 28 '21 08:10 kazupon

Why is this PR still a WIP?

ThomasBerne avatar Dec 09 '21 14:12 ThomasBerne

Hi @rchl,

Do you have any plans to resume work on this PR? This would be of such a great enhancement! 😃

DamianGlowala avatar Feb 16 '22 00:02 DamianGlowala

There is ongoing work on next major version that will include the composition API. Though it's not clear how rough the transition to the next major version will be so maybe worth to still get this in.

rchl avatar Feb 16 '22 07:02 rchl

Hi @rchl,

Would you have a spare moment to see how this draft PR could be tweaked for using with Nuxt 3 & Bridge? Nuxt i18n is the only library that holds me back from making a full transition to Nuxt 3, therefore I am trying to find these workarounds and any advice on this would be really appreciated 😄

That's what I came up with (it throws @nuxtjs/i18n not initialized error when imported in the <script setup> tag):

import Vue from "vue";

export function useI18n() {
    const { nuxt2Context } = useNuxtApp();
    const app = nuxt2Context;
    const i18n = nuxt2Context.$i18n;

    if (!i18n) {
        throw new Error("@nuxtjs/i18n not initialized");
    }

    const instance = getCurrentInstance();
    const vm = instance?.proxy || new Vue({});

    const locale = computed({
        get() {
            return i18n.locale;
        },
        /** @param {string} v */
        set(v) {
            i18n.setLocale(v);
        }
    });

    return {
        ...i18n,
        locale,
        getRouteBaseName: app.getRouteBaseName,
        localeLocation: app.localeLocation,
        localePath: app.localePath,
        localeRoute: app.localeRoute,
        nuxtI18nHead: vm.$nuxtI18nHead.bind(vm),
        switchLocalePath: app.switchLocalePath,
        t: vm.$t.bind(vm),
        tc: vm.$tc.bind(vm),
        d: vm.$d.bind(vm),
        te: vm.$te.bind(vm),
        n: vm.$n.bind(vm)
    };
}

DamianGlowala avatar Mar 03 '22 12:03 DamianGlowala

This change alone won't help much with Nuxt 3 support. There is a complete refactor needed that is happening on the next branch (also see https://github.com/nuxt-community/i18n-module/discussions/1287).

rchl avatar Mar 03 '22 12:03 rchl

Hi @rchl,

Would you have a spare moment to see how this draft PR could be tweaked for using with Nuxt 3 & Bridge? Nuxt i18n is the only library that holds me back from making a full transition to Nuxt 3, therefore I am trying to find these workarounds and any advice on this would be really appreciated 😄

That's what I came up with (it throws @nuxtjs/i18n not initialized error when imported in the <script setup> tag):

import Vue from "vue";

export function useI18n() {
    const { nuxt2Context } = useNuxtApp();
    const app = nuxt2Context;
    const i18n = nuxt2Context.$i18n;

    if (!i18n) {
        throw new Error("@nuxtjs/i18n not initialized");
    }

    const instance = getCurrentInstance();
    const vm = instance?.proxy || new Vue({});

    const locale = computed({
        get() {
            return i18n.locale;
        },
        /** @param {string} v */
        set(v) {
            i18n.setLocale(v);
        }
    });

    return {
        ...i18n,
        locale,
        getRouteBaseName: app.getRouteBaseName,
        localeLocation: app.localeLocation,
        localePath: app.localePath,
        localeRoute: app.localeRoute,
        nuxtI18nHead: vm.$nuxtI18nHead.bind(vm),
        switchLocalePath: app.switchLocalePath,
        t: vm.$t.bind(vm),
        tc: vm.$tc.bind(vm),
        d: vm.$d.bind(vm),
        te: vm.$te.bind(vm),
        n: vm.$n.bind(vm)
    };
}

It seems like in more recent versions is "i18n" instead of "$i18n"

vis97c avatar Mar 11 '22 19:03 vis97c

Thanks for the tip @vis97c! Have you managed to make it work by any chance or this is a pure guess? I am using TypeScript and couldn't see the latter variant. I rolled back to Nuxt 2 without bridge for the time being.

DamianGlowala avatar Mar 11 '22 19:03 DamianGlowala

Thanks for the tip @vis97c! Have you managed to make it work by any chance or this is a pure guess? I am using TypeScript and couldn't see the latter variant. I rolled back to Nuxt 2 without bridge for the time being.

I'm using nuxt with the bridge and is working. image

157958937-b1422807-56ec-415d-b229-b6ef4889a009

Haven't tested on script setup yet. Mainly because is a lot more unstable. For the router meta i'm just storing the keys and later within the vue instance passing them to the i18n function.

image

vis97c avatar Mar 11 '22 20:03 vis97c

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 12 '22 21:06 stale[bot]