如何在语言为韩文或中文时更改特定 UI(QML)的字体?

huangapple go评论92阅读模式
英文:

How to change font for a particular UI (QML) when the language is either Korean or Chinese?

问题

I can help you with the translation part. Here's the translation of your content:

我有以下的QML文档(为简化起见)-

Item {
   Label { // Label1
      text: qsTr("Label1")
   }

   Label { //Label2
      text: qsTr("Label2")
   }
}

我有一个C++类,用于搜索翻译的QM文件,并在用户更改应用程序语言时调用installTranslator。翻译正常工作。

要求是,如果当前语言是韩语或中文,我需要更改“Label1”的字体,但不应更改“Label2”的字体。如果语言不是这两种语言之一,那么“Label1”和“Label2”的字体都应相同。

您能告诉我如何检测当前语言是否为韩语或中文,以及如何获取语言更改时的信号吗?

英文:

I have the following QML document (reduced for simplicity) -

Item {
   Label { // Label1
      text: qsTr("Label1")
   }

   Label { //Label2
      text: qsTr("Label2")
   }
}

I have a C++ class that searches for the translation QM files and call installTranslator when the application language is changed by the user. Translation is working properly.

The requirement is that I need to change the font of Label1 if the current language is Korean or Chinese but the font of Label2 should not change. If the language is not one of those two, the font for both Label1 and Label2 should be same.

Can you please tell me how can I detect if the current language is Korean or Chinese and how can we get a signal that the language has been changed when that happens?

答案1

得分: 2

We can solve this with qsTr().

For instance, we can introduce qsTr("western") which, by default, is "western". For Chinese, (Japanese) and Korean you can use Linguist to change qsTr("western") to, say, "cjk". Then we can use this to help switch between a default font.family versus an asian font family for just Label1 and not Label2.

   Label { // Label1
       text: qsTr("Label1")
       property TextMetrics tm: TextMetrics { }
       font.family: qsTr("western") === "western" ? tm.font.family : "some-cjk-font-family"
   }
   Label { //Label2
       text: qsTr("Label2")
   }

If you wish to apply this logic in multiple places you can refactor some properties that you can bind to:

    property string cjkCheck: qsTr("western") // "western" (default) or "cjk" (Chinese, Japanese, or Korean)
    property bool isCJK: cjkCheck !== "western"
    property TextMetrics tm: TextMetrics { }
    property string cjkFontFamily: isCJK ? "some-cjk-font-family" : tm.font.family

    Label { // Label1
        text: qsTr("Label1")
        font.family: cjkFontFamily
    }
    Label { //Label2
        text: qsTr("Label2")
    }

[EDIT]

Based on your more recent comment, another solution is you can have a LocalizedLabel, e.g.

// LocalizedLabel.qml
import QtQuick
import QtQuick.Controls
Label {
    font.family: qsTr("Arial")
}

Then, you can use the LocalizedLabel when you need it. That means, you get to default to "Arial" or choose a specific font-family and have it all managed by (1) a reusable LocalizedLabel component, (2) manage the font-family via Linguist.

    LocalizedLabel { // Label1
        text: qsTr("Label1")
    }
    Label { //Label2
        text: qsTr("Label2")
    }
英文:

We can solve this with qsTr().

For instance, we can introduce qsTr("western") which, by default, is "western". For Chinese, (Japanese) and Korean you can use Linguist to change qsTr("western") to, say, "cjk". Then we can use this to help switch between a default font.family versus an asian font family for just Label1 and not Label2.

   Label { // Label1
       text: qsTr("Label1")
       property TextMetrics tm: TextMetrics { }
       font.family: qsTr("western") === "western" ? tm.font.family : "some-cjk-font-family"
   }
   Label { //Label2
       text: qsTr("Label2")
   }

If you wish to apply this logic in multiple places you can refactor some properties that you can bind to:

    property string cjkCheck: qsTr("western") // "western" (default) or "cjk" (Chinese, Japanese, or Korean)
    property bool isCJK: cjkCheck !== "western"
    property TextMetrics tm: TextMetrics { }
    property string cjkFontFamily: isCJK ? "some-cjk-font-family" : tm.font.family

    Label { // Label1
        text: qsTr("Label1")
        font.family: cjkFontFamily
    }
    Label { //Label2
        text: qsTr("Label2")
    }

[EDIT]

Based on your more recent comment, another solution is you can have a LocalizedLabel, e.g.

// LocalizedLabel.qml
import QtQuick
import QtQuick.Controls
Label {
    font.family: qsTr("Arial")
}

Then, you can use the LocalizedLabel when you need it. That means, you get to default to "Arial" or choose a specific font-family and have it all managed by (1) a reusable LocalizedLabel component, (2) manage the font-family via Linguist.

    LocalizedLabel { // Label1
        text: qsTr("Label1")
    }
    Label { //Label2
        text: qsTr("Label2")
    }

huangapple
  • 本文由 发表于 2023年6月22日 00:34:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定