简体   繁体   English

使用 xgettext 从 VueJS 文件中提取可翻译的字符串

[英]Using xgettext to extract translatable strings from VueJS file

I am trying to use xgettext to extract strings to be translated from a VueJS file.我正在尝试使用xgettext从 VueJS 文件中提取要翻译的字符串。 I am not able to get xgettext to recognize JS that is in a VueJS computed property though.但是,我无法让xgettext识别 VueJS 计算属性中的 JS。

For example, I have an element in my <template> like so:例如,我的<template>有一个元素,如下所示:

<input :placeholder="translator.gettext('Phone')" />

This fails to get picked up when running xgettext like so:像这样运行xgettext时,这无法被拾取:

xgettext --from-code=UTF-8 --language=JavaScript

But if I have a translatable string as a function call, it is picked up.但是如果我有一个可翻译的字符串作为函数调用,它就会被拾取。 For example:例如:

<div>{{ translator.gettext('This is picked up 1') }}</div>
<input :placeholder="translator.gettext('This is NOT picked up')" />
<div>{{ translator.gettext('This is picked up 2') }}</div>

The input placeholder is not picked up, but the other 2 strings are.未选取input占位符,但会选取其他 2 个字符串。

I believe this is because xgettext considers anything inside an html property to be just a string, but VueJS will run any value in a property prefixed by a : as pure JavaScript.我相信这是因为xgettext认为 html 属性中的任何内容都只是一个字符串,但 VueJS 会将以:为前缀的属性中的任何值作为纯 JavaScript 运行。

Is there any way to get xgettext to understand that this code is JS and not just a string?有没有办法让xgettext理解这段代码是 JS 而不仅仅是一个字符串?

I think translation does not work on attributes.我认为翻译对属性不起作用。 Maybe you can try using a computed property to achieve this like:也许您可以尝试使用计算属性来实现此目的:

computed: {
   placeholderText () {
     return this.translator.gettext('This is a text')
   },
},

and then use this computed property in your template like:然后在您的模板中使用此计算属性,例如:

<input :placeholder="placeholderText" />

Or, you can create a filter for this like:或者,您可以为此创建一个过滤器,例如:

Vue.filter('translate', value => {
  return !value ? '' : Vue.prototype.translator.gettext(value.toString())
})

and then use it like:然后像这样使用它:

<input :placeholder="'This is a text' | translate" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM