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
相關文章
- iOS 多國語言本地化與App內語言切換(Swift)iOSAPPSwift
- 語言本地化
- go語言介面避免無意被適配Go
- windows10:vscode下go語言的適配WindowsVSCodeGo
- Android 國際化之多語言適配小記Android
- Asp.net Core 多語言本地化 一些自己的思路和做法ASP.NET
- Clean Architecture For RazorPage 實現多語言和本地化
- RxRetroHttp,為多套API請求適配而生HTTPAPI
- Android適配: 拉伸適配的缺點Android
- 學習筆記:自適應佈局,多螢幕適配筆記
- Go語言適合做什麼 ?Go
- Oracle 實現多語言(即根據使用者登入的環境自適應本地語言)Oracle
- iOS語言國際化/本地化-實踐總結iOS
- Nature 子刊,化學語言模型自動設計多靶點配體模型
- flutter 螢幕尺寸適配 字型大小適配Flutter
- Qt 多語言支援QT
- ModStart多語言支援
- GPT大語言模型Alpaca-lora本地化部署實踐【大語言模型實踐一】GPT模型
- 鴻蒙適配一多搭建首頁框架鴻蒙框架
- Android適配Android
- dimens適配
- iphoneX適配iPhone
- star 最多的 Go 語言本地化庫|GitHub 2.8KGoGithub
- ICML 2024 | 大語言模型預訓練新前沿:「最佳適配打包」重塑文件處理標準模型
- 鴻蒙Banner圖一多適配不同螢幕鴻蒙
- Flutter多語言實踐Flutter
- Laravel中的多語言Laravel
- APP 多語言測試APP
- Android螢幕適配(理論適配100%機型)Android
- WebView iPhoneX適配WebViewiPhone
- iphoneX,XsMax適配iPhone
- 裝置適配
- 適配問題
- 螢幕適配
- android螢幕適配三:通過畫素密度適配Android
- Flutter多平臺適配機制就是這麼簡單Flutter
- iOS 多語言化之痛iOS
- vue element 多語言設定Vue