GithubHelp home page GithubHelp logo

Comments (4)

BobbieGoede avatar BobbieGoede commented on June 2, 2024

I think such directive usage wouldn't work during SSR, as only the directive binding will be accessible as the node/element does not exist yet, this isn't very clearly described in the Vue docs.

from vue-i18n-next.

alcalyn avatar alcalyn commented on June 2, 2024

Yeah I had also ssr in mind, I don't use it but I guess it is also a friction point for this

from vue-i18n-next.

kazupon avatar kazupon commented on June 2, 2024

FYI:
https://github.com/intlify/vue-i18n-extensions

from vue-i18n-next.

alcalyn avatar alcalyn commented on June 2, 2024

Here is a better version with no warning, and insteadn I use another directive name to prevent trying to override:

<h1 v-t="'Games'"></h1>
=>
<h1 v-trans>Games</h1>

<h3>{{ $t('Online ({n})', { n: 3 }) }}</h3>
=>
<h3 v-trans="{ n: 3 }">Online ({n})</h3>
import { TranslationDirective, vTDirective } from 'vue-i18n';
import { i18n } from '.';
import { DirectiveBinding, DirectiveHook, VNode } from 'vue';

/**
 * Allow a shortcut to translate in a more readable way:
 *
 * - Currently:
 *      `<h1 v-t="'Games'"></h1>`
 * - With this custom directive:
 *      `<h1 v-trans>Games</h1>`
 *
 * or
 *
 * - Currently:
 *      `<h3>{{ $t('Online ({n})', { n: 3 }) }}</h3>`
 * - With this custom directive:
 *      `<h3 v-trans="{ n: 3 }">Online ({n})</h3>`
 *
 * https://github.com/intlify/vue-i18n-next/issues/1820
 */
export const customTrans: TranslationDirective<HTMLElement> = (() => {
    const setValueFromContent = (callback: DirectiveHook<HTMLElement> | undefined) => (el: HTMLElement, binding: DirectiveBinding, vnode: VNode<unknown, HTMLElement>) => {
        if (undefined === callback) {
            return;
        }

        let path: null | string = null;

        if (undefined !== el.dataset.baseTranslationKey) {
            path = el.dataset.baseTranslationKey;
        } else {
            path = el.innerText;
            el.dataset.baseTranslationKey = path;
        }

        if (!binding.value) {
            binding.value = path;
        } else if ('object' === typeof binding.value) {
            binding.value = {
                path,
                args: binding.value,
            };
        } else {
            throw new Error(`Expected either v-trans="'string'" or v-trans="{ key: value }" for element containing "${el.innerText}"`);
        }

        callback(el, binding, vnode, null);
    };

    const directive = vTDirective(i18n);

    directive.beforeUpdate = setValueFromContent(directive.beforeUpdate);
    directive.created = setValueFromContent(directive.created);

    return directive;
})();

but actually I still hesitate using this, or use proper translation keys <p v-trans="{ date: ... }">game.created_at</p> which lower the benefit of this feature

from vue-i18n-next.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.