国际化string
字符串显示的载体Label
Cocos2d-x 3.3中的Label的主要API:
-
Label::createWithTTF,使用libfreetype2创建字体
-
Label::createWithSystemFont,创建系统原生字体
-
Label::createWithCharMap,一般用来显示数字
国际化思路
-
思路:类似android的value-en/string.xml value-zh/string.xml
-
cpp-tests提供的原料:LabelTTFUnicodeNew::LabelTTFUnicodeNew()
国际化实现
1. 根据系统语言判断选取对应的string.xml,简单包装了下,单例,跟label合起来用步骤还是有些繁琐
.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#ifndef__MyStringUtils__
#define__MyStringUtils__
#include"cocos2d.h"
USING_NS_CC;
class
MyStringUtils{
private
:
static
MyStringUtils*instance;
ValueMapstrings;
MyStringUtils();
public
:
MyStringUtils*getInstance();
static
void
destroyInstance();
~MyStringUtils();
std::stringgetStringForKey(
const
std::string&key);
};
#endif
|
.cpp
#include"MyStringUtils.h"
MyStringUtils*MyStringUtils::instance=NULL;
MyStringUtils::MyStringUtils(){
if
(Application::getInstance()->getCurrentLanguage()
==LanguageType::CHInesE){
strings=FileUtils::getInstance()->getValueMapFromFile(
"fonts/strings_zh.xml"
);
}
else
{
strings=FileUtils::getInstance()->getValueMapFromFile(
"fonts/strings_en.xml"
);
}
}
MyStringUtils::~MyStringUtils(){
}
MyStringUtils*MyStringUtils::getInstance(){
(instance==NULL){
instance=
new
MyStringUtils();
}
return
instance;
}
MyStringUtils::destroyInstance()
{
CC_SAFE_DELETE(instance);
}
std::stringMyStringUtils::getStringForKey(
std::string&key){
strings[key].asstring();
}
|
2. 用法举例
{
autosize=Director::getInstance()->getWinSize();
float
vStep=size.height/9;
vSize=size.height;
//传入key,获得对应的字符串
autolabel1=Label::createWithTTF(ttfConfig,MyStringUtils::getInstance()->getStringForKey(
"key_beijing"
),TextHAlignment::CENTER,size.width);
label1->setPosition(Vec2(size.width/2,vSize-(vSteP*4.5)));
addChild(label1);
autolabel2=Label::createWithTTF(ttfConfig,monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important">"key_hello_world"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,size.width);
label2->setPosition(Vec2(size.width/2,vSize-(vSteP*5.5)));
addChild(label2);
autolabel3=Label::createWithTTF(ttfConfig,monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important">"key_beatuy_day"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,size.width);
label3->setPosition(Vec2(size.width/2,vSize-(vSteP*6.5)));
addChild(label3);
3. 看看string.xml
相关推荐php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇
|