cocos2d-x本地化/多語言適配

Norton-Linux核心研究發表於2020-04-07

一、cocos2d-x自帶程式碼介紹

請注意,cocos2d-x的本地化需要在“真機”上測試。其本身沒有整合該解決方案,需要自己設計,唯一提供的函式如下

CCComment.h中的定義:

typedef enum LanguageType
{
    kLanguageEnglish = 0,
    kLanguageChinese,
    kLanguageFrench,
    kLanguageItalian,
    kLanguageGerman,
    kLanguageSpanish,
    kLanguageRussian,
    kLanguageKorean
} ccLanguageType;

CCApplication.cpp中定義如下

#ifndef _CURRENT_LANGUAGE_TEST_H_
#define _CURRENT_LANGUAGE_TEST_H_

#include "cocos2d.h"
#include "../testBasic.h"

class CurrentLanguageTest : public CCLayer
{
public:
    CurrentLanguageTest();
};

class CurrentLanguageTestScene : public TestScene
{
public:
    virtual void runThisTest();
};

#endif // _CURRENT_LANGUAGE_TEST_H_

ccLanguageType CCApplication::getCurrentLanguage()
{
    ccLanguageType ret = kLanguageEnglish;

    LCID localeID = GetUserDefaultLCID();
    unsigned short primaryLanguageID = localeID & 0xFF;
    
    switch (primaryLanguageID)
    {
        case LANG_CHINESE:
            ret = kLanguageChinese;
            break;
        case LANG_ENGLISH:
            ret = kLanguageEnglish;
            break;
        case LANG_FRENCH:
            ret = kLanguageFrench;
            break;
        case LANG_ITALIAN:
            ret = kLanguageItalian;
            break;
        case LANG_GERMAN:
            ret = kLanguageGerman;
            break;
        case LANG_SPANISH:
            ret = kLanguageSpanish;
            break;
        case LANG_RUSSIAN:
            ret = kLanguageRussian;
            break;
        case LANG_KOREAN:
            ret = kLanguageKorean;
            break;
    }

    return ret;
}
使用方法:

	ccLanguageType currentLanguageType = CCApplication::sharedApplication()->getCurrentLanguage();

TestCpp專案中有test例項,參見程式碼CurrentLanguageTest.h/cpp

#ifndef _CURRENT_LANGUAGE_TEST_H_
#define _CURRENT_LANGUAGE_TEST_H_

#include "cocos2d.h"
#include "../testBasic.h"

class CurrentLanguageTest : public CCLayer
{
public:
    CurrentLanguageTest();
};

class CurrentLanguageTestScene : public TestScene
{
public:
    virtual void runThisTest();
};

#endif // _CURRENT_LANGUAGE_TEST_H_


#include "CurrentLanguageTest.h"

CurrentLanguageTest::CurrentLanguageTest()
{
    CCLabelTTF* label = CCLabelTTF::create("Current language Test", "Arial", 28);
    addChild(label, 0);
    label->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y-50) );

    CCLabelTTF *labelLanguage = CCLabelTTF::create("", "Arial", 20);
    labelLanguage->setPosition(VisibleRect::center());

    ccLanguageType currentLanguageType = CCApplication::sharedApplication()->getCurrentLanguage();
    switch (currentLanguageType)
    {
    case kLanguageEnglish:
        labelLanguage->setString("current language is English");
        break;
    case kLanguageChinese:
        labelLanguage->setString("current language is Chinese");
        break;
    case kLanguageFrench:
        labelLanguage->setString("current language is French");
        break;
    case kLanguageGerman:
        labelLanguage->setString("current language is German");
        break;
    case kLanguageItalian:
        labelLanguage->setString("current language is Italian");
        break;
    case kLanguageRussian:
        labelLanguage->setString("current language is Russian");
        break;
    case kLanguageSpanish:
        labelLanguage->setString("current language is Spanish");
        break;
	case kLanguageKorean:
        labelLanguage->setString("current language is Korean");
        break;
    }

    addChild(labelLanguage);
}

void CurrentLanguageTestScene::runThisTest()
{
    CCLayer* pLayer = new CurrentLanguageTest();
    addChild(pLayer);

    CCDirector::sharedDirector()->replaceScene(this);
    pLayer->release();
}

二、資原始檔(包括圖片)適配方法

指定讀取資源的目錄,以後從這裡讀取,例如高清中文,高清英文。一些通用的資源,可以放到Resource下,程式在hd_xx中找不到,會到Resource下查詢。

	CCFileUtils::sharedFileUtils()->setResourceDirectory("hd_ch");
	CCFileUtils::sharedFileUtils()->setResourceDirectory("hd_en");


三、字串資源適配方法

將字串統一放在資原始檔夾中作為資源處理

{

"name":"小名"

"interest":"籃球"

}


{

"name":"ming"

"interest":"basketball"

}

把以上檔案儲存在相同的檔名,不同的資源路徑下 。然後通過以下方法讀取,前提是已經設定了資源路徑(參照本文第二節)。 

unsigned long size ;
	char* file = (char*)CCFileUtils::sharedFileUtils()->getFileData("test.json","r",&size);
	Json::Reader reader;
	Json::Value root;
	if (!reader.parse(std::string(file),root,false))
	{
		return;
	}
	
	std::string name = root["name"].asString();
	std::string interest = root["interest"].asString();
建議封裝成單例模式

以上程式碼為JsonCpp程式碼,配置方法參照之前的一篇文章。配置方法很簡單,只需要把第三方庫檔案匯入你的專案就行。

Json庫配置和使用

當然,也可以用xml實現,參照tinyXml的配置方法:

http://blog.csdn.net/xzongyuan/article/details/9157555

最後,搜到一篇參考文章,還沒細看

http://blog.csdn.net/zyxhjy/article/details/8318529



相關文章