• qt国际化


    参考网友作品,来源难寻,如有不妥请邮件联系: justin.seeley.cn@gmail.com

    Qt的国际化做的非常好。

    1. unicode支持

    a. QString & QChar QString背面原本的思想是用16bit而非8bit来存储character[字符],因此可以支持到65,000左右character.Qt的QString使用unicode来存储字符串,也就是说每一个字符都是一个16bit的QChar而非8bit的char。 QChar提供了相应的方法来完成C中如果isalpha, isdigit等的功能.

    b. text

    当读写text时,涉及文件的编码格式。文件可以编码格式非常多,比如utf-8 , utf-16, ansi等,通常不易从内容猜测出编码格式。缺省的,QTextStream使用本地系统的编码格式,QTextCodec::codecForLocale()。当然这是可以手动设置的:

    stream.setCodec("UTF-16");
    stream.setGenerateByteOrderMark(true);

    第二行,设置在每个字符(16 bit, ie. 2 bytes)前使用一个prefix : 0xFFFE,以表明文件是使用unicode编码及其大小端方式。

    还有一些文件会头部表明自身的编码方式,如

    <?xml version="1.0" encoding="utf-8" ?>
    

    The header is typically plain ASCII to ensure that it is read correctly no matter what encoding is used (assuming that it is a superset of ASCII). The XML file format is an interesting example of this. XML files are normally encoded as UTF-8 or UTF-16. The proper way to read them in is to call setCodec() with "UTF-8". If the format is UTF-16, QTextStream will automatically detect this and adjust itself.如果是XML可以用Qt的XML相关类来处理。

    c. TR()

    TR()后面会详说。缺省的,TR()使用Latin-1的编码方式,但可以通过QTextCodec::setCodecForTr()这一静态函数完成,如:

    QTextCodec::setCodecForTr(QTextCodec::codecForName("EUC-JP"));

    这一调用要在第一个tr()使用前。

    2.多语言

    让程序在多种语言环境下都可以用。

    a.相关函数

    可以先看 b 小节中的步骤,再来看这几个标识函数

    The tr() function is a static function defined in QObject and overridden in every subclass defined with the Q_OBJECT macro.

    The most general way of translating a string in Qt is to use the QApplication::translate() function:

    QApplication::translate("Global Stuff", "Hello Qt!") 

    Although it is generally inadvisable to call tr() on a variable, it can be made to work. We must use the QT_tr_NOOP() macro to mark the string literals for translation before we assign them to a variable.

    b. 步骤

    实现多国语的步骤大体上说来有这么几步:
    i 在需要被翻译的字符串前面标识tr,如QString str=tr(“hello,world!”); ,这很重要,因为翻译工具会把源码中tr标识的字符串提取出来,翻译成其他语言,如果没有用tr标识的,不会被工具提取。在界面中输入的文字,默认已经是加上tr的了,所以在翻译时也能看见。建议:在程序中的字符串使用英文,汉语等通过多国语翻译来实现,而不要采取把汉字写在代码中。
    ii 在工程文件***.pro中,添加一项 TRANSLATIONS +=    ***.ts  ****.ts 扩展名为.ts是翻译的源文件,表示生成这几个文件。一般我们会在命名中把区域加进去,更好的注释这些文件是用于什么语言的,比如中文大多会这样命名 myapp_zh_CN.ts, zh_CN表示的就是中国。
    iii 使用lupdate工具提取翻译源文件, 命令是这样的  #lupdate ***.pro ,lupdate会解析***.pro即工程文件,生成TRANSLATIONS中的 ***.ts 几个文件,这些文件可以被linguist工具打开,按照提示一个一个的翻译成需要的文件,然后保存就OK,

    iv 使用lrelease工具发布翻译文件的二进制文件,这样在程序运行时载入会大大的加快速度。使用方式是#lrelease ***.pro,这个工具会提示你多少语句被翻译,多少被忽略了等。生成的文件是 ***.qm,于同名的 ***.ts只是换了一个扩展名。而这才是我们程序需要使用到的文件。

    v 使用***.qm文件.

    切换语言分为两种情况:

    1. 程序载入的时候,根据当前的区域设置,自动选择语言包(.qm),即可;

    2. 要求在程序运行过程中动态切换语言

    第一种情况,一般在main函数中程序启动的部分加入如下代码:

    QString locale = QLocale::system().name()); // for example: zh_CN, en_US 
    QTranslator *translator = new QTranslator(app); 
    translator->load(QString("./language/" + locale)); // 会在当前目录下的language目录下寻找,可以不带".qm"后缀名 
    app->installTranslator( translator ); //安装翻译器   
    
    

    第二种情况,我们假设有一个QComboBox连接了changeLang的槽:

    connect(langCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeLang(int)) ); // 载入不同的语言包 
    void WizarDialog::changeLang( int langIndex ) { 
    	QTranslator *translator = new QTranslator(qApp);
    	switch( langCombo->currentIndex() ){
    		case 0: 
    			translator->load(QString("./language/pt_BR"));
    			break;
    		case 1: 
    			translator->load(QString("./language/en_US")); 
    			break; 
    		case 2: 
    			translator->load(QString("./language/zh_CN")); 
    		default: 
    			break; 
    	} 
    	qApp->installTranslator( translator );
    	this->initGUI(); 
    } // initGUI() 中会有大量的tr函数 
    
    void WizarDialog::initGUI() { this->setWindowTitle(tr("RTA04W")); /* ...... */ }   

    这两种情况,也可以复合起来用。

    与切换相关的还有其它的工作,参考C++ GUI Programming with Qt Chapter 17 section 2. 后半段

  • 相关阅读:
    提高写作能力
    成长,潜力
    线性代数
    健康的作息时间
    2021-01-06TX技术经验总结.
    Redis的技术总结
    Rocket MQ 原理.
    MBA
    向上管理
    postman怎么打开console控制台,打印console.log
  • 原文地址:https://www.cnblogs.com/justin_s/p/1898028.html
Copyright © 2020-2023  润新知