為何捐款
API 瀏覽器
App Internationalization (i18n)

國際化是一個設計過程,可確保產品(網站或應用程式)可以適應各種語言和地區,而無需對原始碼進行工程變更。 將國際化視為本地化準備。

提示

建議用於處理網站/應用程式的套件是 vue-i18n。 應透過 @quasar/app-vite 啟動檔案@quasar/app-webpack 啟動檔案 新增此套件。 在啟動檔案文件頁面上,您可以看到一個用於插入 vue-i18n 的特定範例。

警告

Quasar 文件假設您已熟悉 vue-i18n。 下面僅說明如何在 Quasar CLI 專案中使用它的基礎知識。 有關其功能的完整列表,請造訪 Vue I18n 文件

手動設定

如果您在 yarn create quasar(或 npm init quasar 或 pnpm 或 Bun 等效命令)精靈中錯過了啟用 i18n,以下是您如何手動設定它的方法。

  1. vue-i18n 依賴項安裝到您的應用程式中。

$ yarn add vue-i18n@next
  1. 建立一個檔案 src/boot/i18n.js,內容如下
import { createI18n } from 'vue-i18n'
import messages from 'src/i18n'

export default ({ app }) => {
  // Create I18n instance
  const i18n = createI18n({
    locale: 'en-US',
    legacy: false, // comment this out if not using Composition API
    messages
  })

  // Tell app to use the I18n instance
  app.use(i18n)
}
  1. 在您的應用程式中建立一個資料夾 (/src/i18n/),該資料夾將保存您將支援的每種語言的定義。 範例:src/i18n。 請注意步驟 2 中的「import messages from 'src/i18n'」。 這是您編寫匯入內容的步驟。

  2. 現在在 quasar.config 中的 boot 區段中參考此檔案

/quasar.config 檔案

return {
  boot: [
    // ...
    'i18n'
  ],

  // ...
}

現在您已準備好在頁面中使用它了。

在 SFC 中設定翻譯區塊

如果我們想要在 Quasar CLI 專案的 SFC(單一檔案元件)內新增對 <i18n> 標籤的支援,那麼我們需要修改現有的配置。

我們首先安裝 @intlify/vue-i18n-loader 套件


$ yarn add --dev @intlify/vue-i18n-loader

然後我們編輯專案根目錄下的 quasar.config 檔案。 我們必須包含以下內容

/quasar.config 檔案

const path = require('node:path')

build: {
  chainWebpack: chain => {
    chain.module
      .rule('i18n-resource')
        .test(/\.(json5?|ya?ml)$/)
          .include.add(path.resolve(__dirname, './src/i18n'))
          .end()
        .type('javascript/auto')
        .use('i18n-resource')
          .loader('@intlify/vue-i18n-loader')
    chain.module
      .rule('i18n')
        .resourceQuery(/blockType=i18n/)
        .type('javascript/auto')
        .use('i18n')
          .loader('@intlify/vue-i18n-loader')
  }
}

如何使用

以下是一個範例,顯示了主要的使用案例

<template>
  <q-page>
    <!-- text interpolation, reactive -->
    {{ $t('hello') }}

    <!-- prop/attr binding, reactive -->
    <q-btn :label="$t('hello')" />

    <!-- v-html directive usage -->
    <span v-html="content"></span>
  </q-page>
</template>

<script setup>
// Composition API variant
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()

// bound to a static variable, non-reactive
// const staticContent = t('hello')
// bound to a reactive variable, but one-time assignment, locale changes will not update the value
// const reactiveStaticContent = ref(t('hello'))

// bound to a reactive variable, locale changes will reflect the value
const content = computed(() => t('hello'))

function notify() {
  Notify.create({
    type: 'positive',
    message: t('hello')
  })
}
</script>
<script>
// Options API variant
export default {
  data() {
    return {
      // bound to a reactive variable, but one-time assignment, locale changes will not update the value
      content: this.$t('hello')
    }
  }
}
</script>

新增語言

假設您想要新增德語。

  1. 建立新檔案 src/i18n/de/index.js 並在那裡複製檔案 src/i18n/en-US/index.js 的內容,然後變更語言字串。
  2. 現在變更 src/i18n/index.js 並在那裡新增新的 de 語言。
import enUS from './en-US'
import de from './de'

export default {
  'en-US': enUS,
  'de': de
}

建立語言切換器

某些 Vue 檔案

<template>
  <!-- ...... -->
  <q-select
    v-model="locale"
    :options="localeOptions"
    label="Quasar Language"
    dense
    borderless
    emit-value
    map-options
    options-dense
    style="min-width: 150px"
  />
  <!-- ...... -->
</template>

<script>
import { useI18n } from 'vue-i18n'

export default {
  setup () {
    const { locale } = useI18n({ useScope: 'global' })

    return {
      locale,
      localeOptions: [
        { value: 'en-US', label: 'English' },
        { value: 'de', label: 'German' }
      ]
    }
  }
}
</script>

大寫

許多語言,例如希臘語、德語和荷蘭語,對於大寫顯示有不直觀的規則,並且有一個您應該注意的邊緣情況

QBtn 元件將使用 CSS text-transform: uppercase 規則自動將其標籤轉換為全部大寫。 根據 MDN webdocs,「語言由 lang HTML 屬性或 xml:lang XML 屬性定義。」 遺憾的是,這在各瀏覽器上的實作並不一致,並且 2017 年 ISO 關於德語大寫 eszett ß 的標準尚未真正普及。 目前您有兩個選項

  1. 在您的標籤中使用 no-caps prop 並寫入應顯示的字串
  2. 在您的標籤中使用 `no-caps` 屬性,並使用 `toLocaleUpperCase` 根據 `$q.lang.getLocale()` 偵測到的地區設定來重寫字串。

偵測地區設定

還有一個方法可以判斷使用者地區設定,此方法由 Quasar 預設提供。

// outside of a Vue file
import { Lang } from 'quasar'
Lang.getLocale() // returns a string

// inside of a Vue file
import { useQuasar } from 'quasar'

setup () {
  const $q = useQuasar()
  $q.lang.getLocale() // returns a string
}

警告

如果您使用 Quasar 的 set 方法(`$q.lang.set()`),這不會反映在上述 Quasar 的 getLocale 中。原因在於 `getLocale()` 將始終返回*使用者*的地區設定(基於瀏覽器設定)。`set()` 方法指的是 Quasar 的內部地區設定,該設定用於確定要使用哪個語言檔案。如果您想查看已使用 `set()` 設定了哪個語言,您可以使用 `$q.lang.isoName`。