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

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

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文档(为简化起见)-

  1. Item {
  2. Label { // Label1
  3. text: qsTr("Label1")
  4. }
  5. Label { //Label2
  6. text: qsTr("Label2")
  7. }
  8. }

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

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

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

英文:

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

  1. Item {
  2. Label { // Label1
  3. text: qsTr("Label1")
  4. }
  5. Label { //Label2
  6. text: qsTr("Label2")
  7. }
  8. }

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.

  1. Label { // Label1
  2. text: qsTr("Label1")
  3. property TextMetrics tm: TextMetrics { }
  4. font.family: qsTr("western") === "western" ? tm.font.family : "some-cjk-font-family"
  5. }
  6. Label { //Label2
  7. text: qsTr("Label2")
  8. }

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

  1. property string cjkCheck: qsTr("western") // "western" (default) or "cjk" (Chinese, Japanese, or Korean)
  2. property bool isCJK: cjkCheck !== "western"
  3. property TextMetrics tm: TextMetrics { }
  4. property string cjkFontFamily: isCJK ? "some-cjk-font-family" : tm.font.family
  5. Label { // Label1
  6. text: qsTr("Label1")
  7. font.family: cjkFontFamily
  8. }
  9. Label { //Label2
  10. text: qsTr("Label2")
  11. }

[EDIT]

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

  1. // LocalizedLabel.qml
  2. import QtQuick
  3. import QtQuick.Controls
  4. Label {
  5. font.family: qsTr("Arial")
  6. }

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.

  1. LocalizedLabel { // Label1
  2. text: qsTr("Label1")
  3. }
  4. Label { //Label2
  5. text: qsTr("Label2")
  6. }
英文:

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.

  1. Label { // Label1
  2. text: qsTr("Label1")
  3. property TextMetrics tm: TextMetrics { }
  4. font.family: qsTr("western") === "western" ? tm.font.family : "some-cjk-font-family"
  5. }
  6. Label { //Label2
  7. text: qsTr("Label2")
  8. }

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

  1. property string cjkCheck: qsTr("western") // "western" (default) or "cjk" (Chinese, Japanese, or Korean)
  2. property bool isCJK: cjkCheck !== "western"
  3. property TextMetrics tm: TextMetrics { }
  4. property string cjkFontFamily: isCJK ? "some-cjk-font-family" : tm.font.family
  5. Label { // Label1
  6. text: qsTr("Label1")
  7. font.family: cjkFontFamily
  8. }
  9. Label { //Label2
  10. text: qsTr("Label2")
  11. }

[EDIT]

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

  1. // LocalizedLabel.qml
  2. import QtQuick
  3. import QtQuick.Controls
  4. Label {
  5. font.family: qsTr("Arial")
  6. }

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.

  1. LocalizedLabel { // Label1
  2. text: qsTr("Label1")
  3. }
  4. Label { //Label2
  5. text: qsTr("Label2")
  6. }

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:

确定