国际化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();
CC_DISALLOW_COPY_AND_ASSIGN(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;
TTFConfigttfConfig(
"fonts/kaiti.ttf"
,23,GlyphCollection::ASCII);
//传入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"
addChild(label2);
autolabel3=Label::createWithTTF(ttfConfig,monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important">"key_beatuy_day"
addChild(label3);
3. 看看string.xml
猜你在找的XML相关文章 |