国际化string.xml,字符串显示的载体Label

国际化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;
ValueMapstrings;
MyStringUtils();
CC_disALLOW_copY_AND_ASSIGN(MyStringUtils);
public :
MyStringUtils*getInstance();
static void destroyInstance();
~MyStringUtils();
std::stringgetStringForKey( const std::string&key);
};
#endif

.cpp

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(){
(instance==NULL){
instance= new MyStringUtils();
}
return instance;
}
MyStringUtils::destroyInstance()
{
CC_SAFE_DELETE(instance);
}
std::stringMyStringUtils::getStringForKey( std::string&key){
strings[key].asstring();
}

2. 用法举例

16
{
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" 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

  • strings_en.xml

12
<?xmlversion= "1.0" encoding= "UTF-8" ?>
<!DOCTYPEplistPUBLIC "-//Apple//DTdplIST1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plistversion= >
<dict>
<key>key_beatuy_day</key>
<string>whatabeautifulday</string>
<key>key_hello_world</key>
<string>hello,world</string>
<key>key_beijing</key>
<string>Beijing</string>
</dict>
</plist>
  • strings_zh.xml

<string>美好的一天</string>
<string>你好世界啊</string>
<string>北京</string>
</plist>

4. ttf字库的选择

  • 最初测试test自带的几个ttf,比如HKYuanMini.ttf。

  • 发现在显示字符串‘世界’时,‘界’显示不出来,于是从网上找了一个楷体字库ttf

  • 更新ttf之后显示正常。 但却带来了另外一个问题,

  • 这个楷体字库居然有10M, 这无疑会增加发布物的大小。

5. 编译

linux编译需要修改CMakeLists.txt

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


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轻松学习总节篇