if (descriptor.template) { // Line 97
htmlParser.parseString(descriptor.template.content, descriptor.filename, {
lineNumberStart: descriptor.template.loc.start.line,
});
}
for file dev/components/DemoTemplate.vue:
<script lang="ts" setup>
import { ref } from 'vue';
const n = ref(1)
function add(x: number) {
if (n.value == 0) { return }
n.value += x
} // here is Line 8. the template tag is Line 10
</script>
<template>
<button @click="add(-1)">-</button>{{ n }} <button @click="add(1)">+</button>
<ul>
<li>{{ __('use `__` as a alias of gettext') }}</li>
<li>
{{ __('use `_n` as a alias of ngettext') }}<br>
{{ _n('One apple','%{n} apples', n, { n:''+ n}) }}
</li>
<li :title="_x('title', 'This is my title')">
{{ __('use `_x` as a alias of pgettext') }}<br>
{{ __('hover here and see the title') }}
</li>
<li>
{{ __('use `_xn` as a alias of npgettext') }}<br>
{{ _xn('list-item','one message','%{n} messages', n, { n:''+ n}) }}
</li>
</ul>
</template>
descriptor.template.content is '\n<button>...'.
When process html tag, getElementContent got a source is
'<head></head><body><button>...</body>'the first \n is lost,
and it's sourceCodeLocation is null.
so jsParser got lineNumberStart = 10, and parse the last _xn call location=13 (line 0 is button, line 13 is _xn)
finally got line number is 23.
When process the last li tag, it's sourceCodeLocation is 13. (AST explorer)
so jsParser got lineNumberStart = 10+13-1 = 22, and parse the last _xn call location=2 (line 0 is '\n', line 1 contains __, line 2 is _xn)
finally got line number is 24.
So if we add the <template> tag to the template content, the problem would be solved:
if (descriptor.template) {
htmlParser.parseString(`<template>${descriptor.template.content}</template>`, descriptor.filename, {
lineNumberStart: descriptor.template.loc.start.line,
});
}
3⃣️ when a key has no translation, the default plural message may calculate wrong
e.g.:
ngettext('One apple', '%{n} apples', n, {n:''+n})
when current lang is zh(which has only one plural form), and n=2,
Hello, I have found some issues:
1⃣️ when the
<template>
is not at beginning of.vue
file, we got wrong line numberI have open a pr on https://github.com/lukasgeiter/gettext-extractor/pull/59, and has released by version 3.6.2, so we can add a
lineNumberStart
on extractor(IHtmlExtractorFunction
).However, I finally found that this repo has not use
HtmlExtractors.embeddedJs
fromgettext-extractor
but write a functionembeddedJsExtractor
, why?Then, I found the
attributeEmbeddedJsExtractor
function, so I copy that to 'the upstream repo' gettext-extractor( see https://github.com/lukasgeiter/gettext-extractor/pull/60)2⃣️ Extract multiple line numbers
e.g.: dev/DemoIf.vue got
but only line 5 has this message.
I found the reason on
scripts/extract.ts
:for file
dev/components/DemoTemplate.vue
:descriptor.template.content
is'\n<button>...'
.When process html tag,
getElementContent
got asource
is'<head></head><body><button>...</body>'
the first\n
is lost, and it'ssourceCodeLocation
is null. so jsParser gotlineNumberStart = 10
, and parse the last_xn
call location=13 (line 0 is button, line 13 is _xn) finally got line number is 23.When process the last li tag, it's sourceCodeLocation is 13. (AST explorer) so jsParser got
lineNumberStart = 10+13-1 = 22
, and parse the last_xn
call location=2 (line 0 is '\n', line 1 contains __, line 2 is _xn) finally got line number is 24.So if we add the
<template>
tag to the template content, the problem would be solved:3⃣️ when a key has no translation, the default plural message may calculate wrong
e.g.:
when current lang is zh(which has only one plural form), and n=2,
the
untranslated
is alwayOne apple
. Seems can be changed to this: