英文:
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")
    }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论