Comments (4)
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.
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.
FYI:
https://github.com/intlify/vue-i18n-extensions
from vue-i18n-next.
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)
- Modifiers for interpolated values
- How to use named interpolation in html HOT 1
- [v9.10.0] `@intlify/message-compiler` publishing missing TypeScript reference HOT 4
- Message compiler still ships with dist with runtime only HOT 3
- te function returns false although fallback exists HOT 5
- Using a implicit fallback throws a warning in the console HOT 1
- Incorrect tm return type HOT 1
- Foolproofing and trigger error, when locale is not set. HOT 2
- regression: wrong `d.ts` of `@intlify/message-compiler` HOT 1
- Hyphens in placeholders causing error `Unterminated closing brace` in vue-i18n@9 HOT 1
- Guide leads to a 404 HOT 4
- Getting 'Cannot read properties of undefined (reading 't')' error in core-base.mjs::formatParts HOT 3
- Linked messages doesn't support chinese character as key HOT 1
- Property "$tc" was accessed during render but is not defined on instance. HOT 1
- CSP Violation when `unsafe-eval` is not set HOT 2
- Key paths containing javascript built-ins are handled inconsistently HOT 3
- Interpolations get lost HOT 2
- SSR without NODE_ENV loads devtools and causes memory leak HOT 7
- Allow custom warn implementation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
Hello
` which will be similar to ``from vue-i18n-next.