jshmrtn / vue3-gettext

Translate Vue 3 applications with gettext.
https://jshmrtn.github.io/vue3-gettext/
MIT License
66 stars 23 forks source link

Fix line number and untranslated plural message #36

Closed youthlin closed 1 year ago

youthlin commented 1 year ago

Hello, I have found some issues:

1⃣️ when the <template> is not at beginning of .vue file, we got wrong line number

I 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 from gettext-extractor but write a function embeddedJsExtractor, 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

#: dev/components/DemoIf.vue:4
#: dev/components/DemoIf.vue:5
msgid "Welcome %{ name }"
msgstr ""

but only line 5 has this message.

I found the reason on scripts/extract.ts:

        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>...'.

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,

   // src/translate.ts
    const untranslated = defaultPlural && plurals.getTranslationIndex(languageKey, n) > 0 ? defaultPlural : msgid;

the untranslated is alway One apple. Seems can be changed to this:

    const untranslated = defaultPlural && n !== 1 ? defaultPlural : msgid;
youthlin commented 1 year ago

@lzurbriggen can you please take a look at this-

youthlin commented 1 year ago

@lzurbriggen remind