在Ubuntu OS上怎么本地化一个QML应用
在这篇文章中,我们来介绍怎么在Ubuntu OS上本地化一个应用。本地化对很多的应用很重要。我们重点介绍怎么把应用本地化为中文。
1)创建一个最基本的应用
# SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2014-11-20 12:05+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <[email protected]>\n" "Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" #: ../app/ui/HelloTab.qml:6 msgid "Hello.." msgstr "您好" #: ../app/ui/HelloTab.qml:18 msgid "Hello World" msgstr "您好,世界" #: ../app/ui/HelloTab.qml:26 msgid "You can change the Tab from Page title above." msgstr "您可以从页面的标题上改变标签" #: ../app/ui/WorldTab.qml:6 msgid "..World!" msgstr "..世界!" #: ../app/ui/WorldTab.qml:18 msgid "WorldTab" msgstr "世界标签" #: /home/liuxg/release/build-localizedqml-Desktop-Default/po/localizedqml.desktop.in.h:1 msgid "LocalizeQml" msgstr "本地化QML"
这里必须注意的是,必须设置charset为UTF-8才可以正常显示中文。我们可以在手机或模拟器中输入一下的命令来查看当前的手机的语言:
Tab { title: i18n.tr("Hello..") page: Page { Column { spacing: units.gu(2) anchors.centerIn: parent HelloComponent { objectName: "helloTab_HelloComponent" anchors.horizontalCenter: parent.horizontalCenter text: i18n.tr("Hello World") } Label { objectName: "helloTab_label" anchors.horizontalCenter: parent.horizontalCenter text: i18n.tr("You can change the Tab from Page title above.") } } } }
我们可以看到所有的字符串是以“i18n.tr”来输出的。它可以帮我们进行本地化。当然我们可以把我们的应用本地化到任何一个其它的语言。
Tab { title: i18n.tr("Hello..") page: Page { Column { spacing: units.gu(2) anchors.centerIn: parent HelloComponent { objectName: "helloTab_HelloComponent" anchors.horizontalCenter: parent.horizontalCenter text: i18n.tr("Hello World") } Label { objectName: "helloTab_label" anchors.horizontalCenter: parent.horizontalCenter text: i18n.tr("You can change the Tab from Page title above.") } Button { text: i18n.tr("Press me") } } } }
重新运行我们的应用,我们看到的是英文的界面。我们也同时查看一下我们在“po”目录下的“com.ubuntu.developer.liu-xiao-guo.localizeqml.pot”,我们看见我们新添加的“Press me”在这个文件中。我们需要重新把它添加到我们的“zh_CN.po”文件中去。
# SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2014-11-20 12:05+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <[email protected]>\n" "Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" #: ../app/ui/HelloTab.qml:6 msgid "Hello.." msgstr "您好" #: ../app/ui/HelloTab.qml:18 msgid "Hello World" msgstr "您好,世界" #: ../app/ui/HelloTab.qml:26 msgid "You can change the Tab from Page title above." msgstr "您可以从页面的标题上改变标签" #: ../app/ui/WorldTab.qml:6 msgid "..World!" msgstr "..世界!" #: ../app/ui/WorldTab.qml:18 msgid "WorldTab" msgstr "世界标签" #: /home/liuxg/release/build-localizedqml-Desktop-Default/po/localizedqml.desktop.in.h:1 msgid "LocalizeQml" msgstr "本地化QML" #: ../app/ui/HelloTab.qml:30 msgid "Press me" msgstr "按一下我"
重新运行我们的应用:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。