Skip to content

cocos2d x 3.3 016 国际化string.xml

cheyiliu edited this page Jan 25, 2015 · 9 revisions

国际化string

字符串显示的载体Label

  • 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 #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 #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. 用法举例

{ 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

key_beatuy_day what a beautiful day key_hello_world hello, world key_beijing Beijing

strings_zh.xml

key_beatuy_day 美好的一天 key_hello_world 你好世界啊 key_beijing 北京 ``` * 4. ttf字库的选择 ``` 最初测试test自带的几个ttf,比如HKYuanMini.ttf。 发现在显示字符串‘世界’时,‘界’显示不出来,于是从网上找了一个楷体字库ttf 更新ttf之后显示正常。 但却带来了另外一个问题, 这个楷体字库居然有10M, 这无疑会增加发布物的大小。。。 ``` * 5. 编译 ``` linux编译需要修改CMakeLists.txt 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=2281
Clone this wiki locally