cocos2d-x本地化/多語言適配
一、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程式碼,配置方法參照之前的一篇文章。配置方法很簡單,只需要把第三方庫檔案匯入你的專案就行。
當然,也可以用xml實現,參照tinyXml的配置方法:
http://blog.csdn.net/xzongyuan/article/details/9157555
最後,搜到一篇參考文章,還沒細看
http://blog.csdn.net/zyxhjy/article/details/8318529
相關文章
- MFC 多語言 本地化
- 語言本地化
- iOS 多國語言本地化與App內語言切換(Swift)iOSAPPSwift
- go語言介面避免無意被適配Go
- iOS應用程式多語言&本地化解決方案iOS
- Android 國際化之多語言適配小記Android
- windows10:vscode下go語言的適配WindowsVSCodeGo
- iOS語言國際化(本地化)iOS
- Asp.net Core 多語言本地化 一些自己的思路和做法ASP.NET
- Cocos與Cocos2d-x協作教程——多解析度適配
- RxRetroHttp,為多套API請求適配而生HTTPAPI
- 學習筆記:自適應佈局,多螢幕適配筆記
- Android適配: 拉伸適配的缺點Android
- flutter 螢幕尺寸適配 字型大小適配Flutter
- Go語言適合做什麼 ?Go
- Android 適配多種ROM的快捷方式Android
- Android 適配多種 ROM 的快捷方式Android
- iOS語言國際化/本地化-實踐總結iOS
- Oracle 實現多語言(即根據使用者登入的環境自適應本地語言)Oracle
- Nature 子刊,化學語言模型自動設計多靶點配體模型
- Qt 多語言支援QT
- ModStart多語言支援
- Clean Architecture For RazorPage 實現多語言和本地化
- star 最多的 Go 語言本地化庫|GitHub 2.8KGoGithub
- 適合IT經理的程式語言
- GPT大語言模型Alpaca-lora本地化部署實踐【大語言模型實踐一】GPT模型
- Android適配Android
- iOS 11適配iOS
- https適配HTTP
- 適配iphone XiPhone
- Flutter多語言實踐Flutter
- struts解決多語言
- Laravel中的多語言Laravel
- Flutter多平臺適配機制就是這麼簡單Flutter
- android螢幕適配三:通過畫素密度適配Android
- Android螢幕適配(理論適配100%機型)Android
- 初創企業產品需要適配多少種語言?到2020年需要48種
- Flutter螢幕適配Flutter