在Vue-i18n中为参数应用样式。

huangapple go评论43阅读模式
英文:

Apply style to parameter in Vue-i18n

问题

Sure, here's the translated part:

在模板中有以下翻译:

<p>
{{ $t('Counter: {n}', {n: counter}) }}
</p>

其中counter只是从脚本返回的一个数字,我想要为“n”应用样式(例如,使其变为红色)。

我如何实现这个目标?

英文:

Having the following translation in the template:

<p>
{{ $t('Counter: {n}', {n: counter}) }}
</p>

where counter is just a number returned from the script, I would like to apply a style to "n" (to make it red, for example).

How can I achieve this?

答案1

得分: 1

translation:

counter: '将HTML直接添加到翻译中可能是一种方法。这将使计数器始终以红色显示:'

template:

<span v-html="$t('counter', {n: 22})" />

如果您希望更加灵活,以便更改颜色,您可以添加附加参数:

<span v-html="$t('counter', {n: 22, color: 'green'})" />

counter: '计数器是:{n}'

英文:

One approach could be adding HTML directly to the translation. This would make the counter always appear in red:

translation:

counter: &#39;Counter is: &lt;span style=&quot;color: red&quot;&gt;{n}&lt;/span&gt;&#39;

template:

&lt;span v-html=&quot;$t(&#39;counter&#39;, {n: 22})&quot; /&gt;

If you want it more flexible regarding color, you could add additional parameter:

&lt;span v-html=&quot;$t(&#39;counter&#39;, {n: 22, color: &#39;green&#39;})&quot; /&gt;
counter: &#39;Counter is: &lt;span style=&quot;color: {color}&quot;&gt;{n}&lt;/span&gt;&#39;

huangapple
  • 本文由 发表于 2023年5月23日 00:08:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定