国际化string
字符串显示的载体Label
Cocos2d-x 3.3中的Label的主要API:
-
Label::createWithTTF, 使用libfreetype2创建字体
-
Label::createWithBMFont, 使用FNT文件创建一个标签
-
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;
ValueMap strings;
MyStringUtils();
CC_DISALLOW_COPY_AND_ASSIGN(MyStringUtils);
public
:
static
MyStringUtils * getInstance();
static
void
destroyInstance();
~MyStringUtils();
std::string getStringForKey(
const
std::string& key);
};
#endif
|
.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#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() {
if
(instance == NULL) {
instance =
new
MyStringUtils();
}
return
instance;
}
void
MyStringUtils::destroyInstance()
{
CC_SAFE_DELETE(instance);
}
std::string MyStringUtils::getStringForKey(
const
std::string& key) {
return
strings[key].asString();
}
|
2. 用法举例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
{
auto size = Director::getInstance()->getWinSize();
float
vStep = size.height/9;
float
vSize = size.height;
TTFConfig ttfConfig(
"fonts/kaiti.ttf"
, 23,GlyphCollection::ASCII);
//传入key,获得对应的字符串
auto label1 = Label::createWithTTF(ttfConfig,MyStringUtils::getInstance()->getStringForKey(
"key_beijing"
), TextHAlignment::CENTER, size.width);
label1->setPosition( Vec2(size.width/2, vSize - (vStep * 4.5)) );
addChild(label1);
auto label2 = Label::createWithTTF(ttfConfig,MyStringUtils::getInstance()->getStringForKey(
"key_hello_world"
), TextHAlignment::CENTER,size.width);
label2->setPosition( Vec2(size.width/2, vSize - (vStep * 5.5)) );
addChild(label2);
auto label3 = Label::createWithTTF(ttfConfig,MyStringUtils::getInstance()->getStringForKey(
"key_beatuy_day"
), TextHAlignment::CENTER,size.width);
label3->setPosition( Vec2(size.width/2, vSize - (vStep * 6.5)) );
addChild(label3);
}
|
3. 看看string.xml
-
strings_en.xml
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE plist PUBLIC
"-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd"
>
<plist version=
"1.0"
>
<dict>
<key>key_beatuy_day</key>
<string>what a beautiful day</string>
<key>key_hello_world</key>
<string>hello, world</string>
<key>key_beijing</key>
<string>Beijing</string>
</dict>
</plist>
|
-
strings_zh.xml
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE plist PUBLIC
"-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd"
>
<plist version=
"1.0"
>
<dict>
<key>key_beatuy_day</key>
<string>美好的一天</string>
<key>key_hello_world</key>
<string>你好世界啊</string>
<key>key_beijing</key>
<string>北京</string>
</dict>
</plist>
|
4. ttf字库的选择
-
最初测试test自带的几个ttf,比如HKYuanMini.ttf。
-
发现在显示字符串‘世界’时,‘界’显示不出来,于是从网上找了一个楷体字库ttf
-
更新ttf之后显示正常。 但却带来了另外一个问题,
-
这个楷体字库居然有10M, 这无疑会增加发布物的大小。
5. 编译
linux编译需要修改CMakeLists.txt
1
2
3
4
5
6
|
set(SAMPLE_SRC
${PLATFORM_SRC}
Classes/AppDelegate.cpp
Classes/HelloWorldScene.cpp
Classes/MyStringUtils.cpp
//增加新文件
)
|
android工程需要在Android.mk
添加新增文件
6. 内存问题(可选)
得在导演这里Director::purgeDirector()调用MyStringUtils::destroyInstance
顺便看看原来这里有多热闹
980: AnimationCache::destroyInstance();
981: SpriteFrameCache::destroyInstance();
982: GLProgramCache::destroyInstance();
983: GLProgramStateCache::destroyInstance();
984: FileUtils::destroyInstance();
987: UserDefault::destroyInstance();
尊重原创,源地址:http://cn.cocos2d-x.org/tutorial/show?id=2371