在Vue 3.2的setup腳本中使用組合式API時,vue-i18n-next中的變數無法運作
P粉420868294
P粉420868294 2023-12-05 22:04:07
0
1
541

在 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粉420868294
P粉420868294

全部回覆(1)
P粉434996845

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

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

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

t('conclusion', 1, { named: { ... } })
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!