首頁  >  問答  >  主體

在Vue 3.2的setup腳本中使用組合式API時,vue-i18n-next中的變數無法運作

在 vue 3.2 專案中,我正在使用 vue-i18n-next v9、composition api<script setup> 方法。

我正在嘗試使用 {}'s 指示的一些變數來建立翻譯訊息。例如

export default {
    conclusion: `After reviewing the above invoices, it was found that there is currently a difference of about {total_difference} in your {advantage_type} on the whole of these invoices.`
}

我的 vue 元件如下所示:

<script setup>
    import { ref } from 'vue';
    import { useI18n } from 'vue-i18n';
    import { getHumanPrice } from '@/utils/helpers';

    const { t } = useI18n();

    const total_difference = ref(2000);

    const conclusion = computed(() => {
        return t('conclusion', 1, {
            total_difference: total_difference.value
            advantage_type: total_difference.value >= 0 ? t('advantage', 1).toLowerCase() : t('disadvantage', 1).toLowerCase(),
        });
    });
</script>

結果是一個不帶可變部分的字串查看上述發票後發現,目前這些發票整體上與您的存在大約差異

以前,在 vue 2 中使用 vue-i18n-next 套件和 options api 時,沒有問題,且字串與變數正確形成。程式碼如下所示:

<script>
    export default: {
        data() {
            return {
                total_difference: 2000,
            }
        },
        computed: {
            conclusion() {
                return this.$tc('conclusion', 1, {
                    total_difference: this.$filters.getHumanPrice(this.total_difference, 2),
                    advantage_type: this.total_difference >= 0 ? t('advantage', 1).toLowerCase() : t('disadvantage', 1).toLowerCase(),
                });
            },
        }
    }
</script>

輸出為 查看上述發票後,發現目前您在這些發票整體上的優勢存在約 $2500,00 的差異。

知道組合 api 方法發生了什麼變化或我可能做錯了什麼嗎?

P粉420868294P粉420868294318 天前543

全部回覆(1)我來回復

  • P粉434996845

    P粉4349968452023-12-06 12:47:31

    $tc 提供了複數翻譯,它的直接替代品是 tc:

    tc('conclusion', 1, { ... })

    t 是通用翻譯函數,雖然它允許使用複數,但它不能接受命名插值的物件作為參數,因為可能有其他選項,在本例中應該是:

    t('conclusion', 1, { named: { ... } })

    回覆
    0
  • 取消回覆