From 6b4a0f808f786dc6afe588d6f5e419b29ca1031d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E5=B3=B0?= Date: Fri, 26 Feb 2016 17:15:24 +0800 Subject: [PATCH] Add cocostudio localization editor export data parser and demo --- build/cocos2d_libs.xcodeproj/project.pbxproj | 16 +++ cocos/2d/libcocos2d.vcxproj | 6 +- cocos/2d/libcocos2d.vcxproj.filters | 10 +- .../libcocos2d_8_1.Shared.vcxitems | 6 +- .../libcocos2d_8_1.Shared.vcxitems.filters | 10 +- cocos/2d/libcocos2d_win10/libcocos2d.vcxproj | 2 + .../libcocos2d.vcxproj.filters | 10 +- cocos/editor-support/cocostudio/Android.mk | 1 + .../editor-support/cocostudio/CMakeLists.txt | 1 + .../cocostudio/LocalizationManager.cpp | 99 ++++++++++++++++ .../cocostudio/LocalizationManager.h | 74 ++++++++++++ .../Classes/LabelTest/LabelTestNew.cpp | 108 ++++++++++++++++++ .../Classes/LabelTest/LabelTestNew.h | 18 +++ .../Resources/configs/CHINESE.lang.csb | Bin 0 -> 84 bytes .../Resources/configs/ENGLISH.lang.csb | Bin 0 -> 80 bytes .../Resources/configs/JAPANESE.lang.csb | Bin 0 -> 92 bytes .../Resources/configs/en-US.lang.json | 3 + .../Resources/configs/ja-JP.lang.json | 3 + .../Resources/configs/zh-CN.lang.json | 3 + 19 files changed, 360 insertions(+), 10 deletions(-) create mode 100644 cocos/editor-support/cocostudio/LocalizationManager.cpp create mode 100644 cocos/editor-support/cocostudio/LocalizationManager.h create mode 100644 tests/cpp-tests/Resources/configs/CHINESE.lang.csb create mode 100644 tests/cpp-tests/Resources/configs/ENGLISH.lang.csb create mode 100644 tests/cpp-tests/Resources/configs/JAPANESE.lang.csb create mode 100644 tests/cpp-tests/Resources/configs/en-US.lang.json create mode 100644 tests/cpp-tests/Resources/configs/ja-JP.lang.json create mode 100644 tests/cpp-tests/Resources/configs/zh-CN.lang.json diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index 45d04821e7..d71f9c1257 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -3745,6 +3745,12 @@ 85505F0E1B60E3DB003F2CD4 /* SkeletonNodeReader.h in Headers */ = {isa = PBXBuildFile; fileRef = C50306741B60B5B2001E6D43 /* SkeletonNodeReader.h */; }; 85B3743A1B204B9400C488D6 /* clipper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 85B374381B204B9400C488D6 /* clipper.cpp */; }; 85B3743B1B204B9400C488D6 /* clipper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 85B374381B204B9400C488D6 /* clipper.cpp */; }; + 94A6DF051C7303FD0094AEF7 /* LocalizationManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 94A6DF031C7303FD0094AEF7 /* LocalizationManager.cpp */; }; + 94A6DF061C7303FD0094AEF7 /* LocalizationManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 94A6DF041C7303FD0094AEF7 /* LocalizationManager.h */; }; + 94A6DF071C73040D0094AEF7 /* LocalizationManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 94A6DF031C7303FD0094AEF7 /* LocalizationManager.cpp */; }; + 94A6DF081C73040E0094AEF7 /* LocalizationManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 94A6DF031C7303FD0094AEF7 /* LocalizationManager.cpp */; }; + 94A6DF091C7304120094AEF7 /* LocalizationManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 94A6DF041C7303FD0094AEF7 /* LocalizationManager.h */; }; + 94A6DF0A1C7304120094AEF7 /* LocalizationManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 94A6DF041C7303FD0094AEF7 /* LocalizationManager.h */; }; A045F6D61BA81577005076C7 /* CCTextureCube.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A045F6D41BA81577005076C7 /* CCTextureCube.cpp */; }; A045F6D71BA81577005076C7 /* CCTextureCube.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A045F6D41BA81577005076C7 /* CCTextureCube.cpp */; }; A045F6D81BA81577005076C7 /* CCTextureCube.h in Headers */ = {isa = PBXBuildFile; fileRef = A045F6D51BA81577005076C7 /* CCTextureCube.h */; }; @@ -6580,6 +6586,8 @@ 5E9F61251A3FFE3D0038DE01 /* CCPlane.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCPlane.h; sourceTree = ""; }; 8525E3A11B291E42008EE815 /* clipper.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = clipper.hpp; sourceTree = ""; }; 85B374381B204B9400C488D6 /* clipper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clipper.cpp; sourceTree = ""; }; + 94A6DF031C7303FD0094AEF7 /* LocalizationManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LocalizationManager.cpp; sourceTree = ""; }; + 94A6DF041C7303FD0094AEF7 /* LocalizationManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocalizationManager.h; sourceTree = ""; }; A045F6D41BA81577005076C7 /* CCTextureCube.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCTextureCube.cpp; sourceTree = ""; }; A045F6D51BA81577005076C7 /* CCTextureCube.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTextureCube.h; sourceTree = ""; }; A045F6DA1BA816A1005076C7 /* CCCameraBackgroundBrush.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCCameraBackgroundBrush.cpp; sourceTree = ""; }; @@ -8368,6 +8376,8 @@ 1A8C5946180E930E00EF57C3 /* cocostudio */ = { isa = PBXGroup; children = ( + 94A6DF031C7303FD0094AEF7 /* LocalizationManager.cpp */, + 94A6DF041C7303FD0094AEF7 /* LocalizationManager.h */, 382384021A259005002C4610 /* CSParseBinary_generated.h */, 182C5CAD1A95961600C30D34 /* CSParse3DBinary_generated.h */, 382384051A25900F002C4610 /* FlatBuffersSerialize.cpp */, @@ -11142,6 +11152,7 @@ 15AE187F19AAD33D00C27E9E /* CCBKeyframe.h in Headers */, B665E2B41AA80A6500DDB1C5 /* CCPUForceField.h in Headers */, 1A570073180BC5A10088DEC7 /* CCActionGrid.h in Headers */, + 94A6DF061C7303FD0094AEF7 /* LocalizationManager.h in Headers */, 15AE1BCC19AAE01E00C27E9E /* CCControlButton.h in Headers */, B6CAB3D71AF9AA1A00B9B856 /* btSolve2LinearConstraint.h in Headers */, C50306771B60B5B2001E6D43 /* CSBoneBinary_generated.h in Headers */, @@ -12036,6 +12047,7 @@ 507B3D3A1C31BDD30067B53E /* CCEventListenerAssetsManagerEx.h in Headers */, 507B3D3B1C31BDD30067B53E /* btGpu3DGridBroadphase.h in Headers */, 507B3D3C1C31BDD30067B53E /* CCAllocatorGlobal.h in Headers */, + 94A6DF0A1C7304120094AEF7 /* LocalizationManager.h in Headers */, 507B3D3D1C31BDD30067B53E /* btSolverConstraint.h in Headers */, 507B3D3E1C31BDD30067B53E /* SpuDoubleBuffer.h in Headers */, 507B3D3F1C31BDD30067B53E /* CCPUScriptCompiler.h in Headers */, @@ -13043,6 +13055,7 @@ 15B3708319EE414C00ABE682 /* CCEventListenerAssetsManagerEx.h in Headers */, B6CAB4361AF9AA1A00B9B856 /* btGpu3DGridBroadphase.h in Headers */, D0FD03521A3B51AA00825BB5 /* CCAllocatorGlobal.h in Headers */, + 94A6DF091C7304120094AEF7 /* LocalizationManager.h in Headers */, B6CAB3DC1AF9AA1A00B9B856 /* btSolverConstraint.h in Headers */, B6CAB4B61AF9AA1A00B9B856 /* SpuDoubleBuffer.h in Headers */, B665E3D11AA80A6600DDB1C5 /* CCPUScriptCompiler.h in Headers */, @@ -14687,6 +14700,7 @@ 1A570354180BD0B00088DEC7 /* ioapi.cpp in Sources */, 15B3707819EE414C00ABE682 /* AssetsManagerEx.cpp in Sources */, 1A570358180BD0B00088DEC7 /* unzip.cpp in Sources */, + 94A6DF051C7303FD0094AEF7 /* LocalizationManager.cpp in Sources */, B665E3861AA80A6500DDB1C5 /* CCPUPathFollowerTranslator.cpp in Sources */, B29A7DCB19EE1B7700872B35 /* Skeleton.c in Sources */, 15AE196E19AAD35700C27E9E /* CCActionTimelineCache.cpp in Sources */, @@ -15530,6 +15544,7 @@ 507B3C141C31BDD30067B53E /* CCPUBoxCollider.cpp in Sources */, 507B3C151C31BDD30067B53E /* CCPUMeshSurfaceEmitter.cpp in Sources */, 507B3C161C31BDD30067B53E /* CCPUOnQuotaObserverTranslator.cpp in Sources */, + 94A6DF081C73040E0094AEF7 /* LocalizationManager.cpp in Sources */, 507B3C171C31BDD30067B53E /* UIPageView.cpp in Sources */, 507B3C181C31BDD30067B53E /* CCComponentContainer.cpp in Sources */, 507B3C191C31BDD30067B53E /* ccCArray.cpp in Sources */, @@ -16373,6 +16388,7 @@ B665E22B1AA80A6500DDB1C5 /* CCPUBoxCollider.cpp in Sources */, B665E3031AA80A6500DDB1C5 /* CCPUMeshSurfaceEmitter.cpp in Sources */, B665E3571AA80A6500DDB1C5 /* CCPUOnQuotaObserverTranslator.cpp in Sources */, + 94A6DF071C73040D0094AEF7 /* LocalizationManager.cpp in Sources */, 15AE1B7619AADA9A00C27E9E /* UIPageView.cpp in Sources */, 1A570311180BCF190088DEC7 /* CCComponentContainer.cpp in Sources */, 50ABBE2C1925AB6F00A911A9 /* ccCArray.cpp in Sources */, diff --git a/cocos/2d/libcocos2d.vcxproj b/cocos/2d/libcocos2d.vcxproj index 18307f2881..9802b9a5d8 100644 --- a/cocos/2d/libcocos2d.vcxproj +++ b/cocos/2d/libcocos2d.vcxproj @@ -1,4 +1,4 @@ - + @@ -526,6 +526,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.* + @@ -1130,6 +1131,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.* + @@ -1387,4 +1389,4 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.* - + \ No newline at end of file diff --git a/cocos/2d/libcocos2d.vcxproj.filters b/cocos/2d/libcocos2d.vcxproj.filters index 173186b11f..85e50cf011 100644 --- a/cocos/2d/libcocos2d.vcxproj.filters +++ b/cocos/2d/libcocos2d.vcxproj.filters @@ -1,4 +1,4 @@ - + @@ -1941,6 +1941,9 @@ network + + cocostudio\json + @@ -3791,6 +3794,9 @@ cocostudio\json + + cocostudio\json + @@ -3819,4 +3825,4 @@ 3d - + \ No newline at end of file diff --git a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems index 31c39d506c..aa1eff799c 100644 --- a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems +++ b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems @@ -1,4 +1,4 @@ - + $(MSBuildAllProjects);$(MSBuildThisFileFullPath) @@ -399,6 +399,7 @@ + @@ -1011,6 +1012,7 @@ + @@ -1288,4 +1290,4 @@ - + \ No newline at end of file diff --git a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters index b82b2259d5..fc7fce09ef 100644 --- a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters +++ b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters @@ -1,4 +1,4 @@ - + @@ -1877,6 +1877,9 @@ cocostudio\reader\WidgetReader\Light3DReader + + cocostudio\components + @@ -3589,6 +3592,9 @@ cocostudio\reader\WidgetReader\Light3DReader + + cocostudio\components + @@ -3985,4 +3991,4 @@ - + \ No newline at end of file diff --git a/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj b/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj index edc25e7396..33ece75dc9 100644 --- a/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj +++ b/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj @@ -509,6 +509,7 @@ + @@ -1127,6 +1128,7 @@ + diff --git a/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj.filters b/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj.filters index 4bfeeae4aa..41db81df9b 100644 --- a/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj.filters +++ b/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj.filters @@ -1,4 +1,4 @@ - + @@ -1978,6 +1978,9 @@ cocostudio\reader\WidgetReader\Light3DReader + + cocostudio\components + @@ -3863,6 +3866,9 @@ cocostudio\reader\WidgetReader\Light3DReader + + cocostudio\components + @@ -4023,4 +4029,4 @@ 3d - + \ No newline at end of file diff --git a/cocos/editor-support/cocostudio/Android.mk b/cocos/editor-support/cocostudio/Android.mk index 1f687c968f..9e4bd9d7f6 100644 --- a/cocos/editor-support/cocostudio/Android.mk +++ b/cocos/editor-support/cocostudio/Android.mk @@ -34,6 +34,7 @@ CCComController.cpp \ CCComRender.cpp \ CCInputDelegate.cpp \ DictionaryHelper.cpp \ +LocalizationManager.cpp \ CCSGUIReader.cpp \ CCSSceneReader.cpp \ TriggerBase.cpp \ diff --git a/cocos/editor-support/cocostudio/CMakeLists.txt b/cocos/editor-support/cocostudio/CMakeLists.txt index 2a23e30193..0f29a0d584 100644 --- a/cocos/editor-support/cocostudio/CMakeLists.txt +++ b/cocos/editor-support/cocostudio/CMakeLists.txt @@ -39,6 +39,7 @@ set(COCOS_CS_SRC editor-support/cocostudio/CCUtilMath.cpp editor-support/cocostudio/CocoLoader.cpp editor-support/cocostudio/DictionaryHelper.cpp + editor-support/cocostudio/LocalizationManager.cpp editor-support/cocostudio/TriggerBase.cpp editor-support/cocostudio/TriggerMng.cpp editor-support/cocostudio/TriggerObj.cpp diff --git a/cocos/editor-support/cocostudio/LocalizationManager.cpp b/cocos/editor-support/cocostudio/LocalizationManager.cpp new file mode 100644 index 0000000000..9980fe8503 --- /dev/null +++ b/cocos/editor-support/cocostudio/LocalizationManager.cpp @@ -0,0 +1,99 @@ +#include "LocalizationManager.h" +#include "CCPlatformMacros.h" +#include "platform/CCFileUtils.h" +#include "editor-support/cocostudio/CSLanguageDataBinary_generated.h" + +using namespace cocostudio; +using namespace cocos2d; + +JsonLocalizationManager::JsonLocalizationManager() + :languageData(nullptr) +{ +} + +JsonLocalizationManager::~JsonLocalizationManager() +{ + CC_SAFE_DELETE(languageData); +} + +bool JsonLocalizationManager::initLanguageData(std::string file) +{ + bool result = false; + + std::string data = FileUtils::getInstance()->getStringFromFile(file); + if (!data.empty()) + { + if (!languageData) + languageData = new rapidjson::Document; + languageData->Parse(data.c_str()); + if (languageData->IsObject()) + result = true; + else + CC_SAFE_DELETE(languageData); + } + + return result; +} + +std::string JsonLocalizationManager::getLocalizationString(std::string key) +{ + std::string result = key; + + if (languageData && languageData->HasMember(key.c_str()) && + (*languageData)[key.c_str()].IsString()) + result = (*languageData)[key.c_str()].GetString(); + + return result; +} + +BinLocalizationManager::BinLocalizationManager() +{ + +} + +BinLocalizationManager::~BinLocalizationManager() +{ + +} + +bool BinLocalizationManager::initLanguageData(std::string file) +{ + bool result = false; + languageData.clear(); + + Data buf = FileUtils::getInstance()->getDataFromFile(file); + if (!buf.isNull()) + { + auto lanSet = flatbuffers::GetLanguageSet(buf.getBytes()); + if (lanSet && lanSet->languageItems()->size() > 0) + { + result = true; + auto items = lanSet->languageItems(); + int count = items->size(); + for (int i = 0; i < count; i++) + { + auto it = items->Get(i); + std::string key = it->key()->c_str(); + std::string value = it->value()->c_str(); + if (!key.empty()) + languageData[key] = value; + } + } + } + + return result; +} + +std::string BinLocalizationManager::getLocalizationString(std::string key) +{ + std::string result = key; + + if (!languageData.empty()) + { + auto it = languageData.find(key); + if (it != languageData.end()) + result = it->second; + } + + return result; +} diff --git a/cocos/editor-support/cocostudio/LocalizationManager.h b/cocos/editor-support/cocostudio/LocalizationManager.h new file mode 100644 index 0000000000..055d291783 --- /dev/null +++ b/cocos/editor-support/cocostudio/LocalizationManager.h @@ -0,0 +1,74 @@ +#ifndef __LOCALLIZATION_MANAGER_H__ +#define __LOCALLIZATION_MANAGER_H__ + +#include +#include +#include "json/document.h" +#include "cocostudio/CocosStudioExport.h" + +namespace cocostudio { + /** + *@brief Localization string manager interface template. + */ + class ILocalizationManager + { + public: + virtual bool initLanguageData(std::string file) = 0; + virtual std::string getLocalizationString(std::string key) = 0; + }; + + /** + *@brief Localization string manager for output Json data file by cocostudio localization editor. + * Note: If changed localization data file manually, please make sure the data file save as + * text file format with encoding as 'UTF8 no BOM', otherwise the localization data may + * not been parse successfully. + */ + class CC_STUDIO_DLL JsonLocalizationManager : ILocalizationManager + { + public: + JsonLocalizationManager(); + ~JsonLocalizationManager(); + + public: + /* Init manager with special localize json data file. + * @param file Name of localize file. + * @return If manager initialize success return true. + */ + virtual bool initLanguageData(std::string file); + + /* Get localization string for special key. + * @param key Special key to search in localization data. + * @return If manager find the key in localization data, return value + * set to key, otherwise return key itself. + */ + virtual std::string getLocalizationString(std::string key); + + protected: + rapidjson::Document * languageData; + }; + + class CC_STUDIO_DLL BinLocalizationManager : ILocalizationManager + { + public: + BinLocalizationManager(); + ~BinLocalizationManager(); + + /* Init manager with special localize binary data file. + * @param file Name of localize file. + * @return If manager initialize success return true. + */ + virtual bool initLanguageData(std::string file); + + /* Get localization string for special key. + * @param key Special key to search in localization data. + * @return If manager find the key in localization data, return value + * set to key, otherwise return key itself. + */ + virtual std::string getLocalizationString(std::string key); + + protected: + std::unordered_map languageData; + }; +} + +#endif //__LOCALLIZATION_MANAGER_H__ \ No newline at end of file diff --git a/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp b/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp index 7410b5e7c7..2f70f6dfaa 100644 --- a/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp +++ b/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp @@ -105,6 +105,8 @@ NewLabelTests::NewLabelTests() ADD_TEST_CASE(LabelUnderlineMultiline); ADD_TEST_CASE(LabelItalics); ADD_TEST_CASE(LabelBold); + + ADD_TEST_CASE(LabelLocalizationTest); }; LabelFNTColorAndOpacity::LabelFNTColorAndOpacity() @@ -3053,3 +3055,109 @@ std::string LabelStrikethrough::subtitle() const return "Strikethrough on TTF and BMfont with multiline"; } +LabelLocalizationTest::LabelLocalizationTest() +{ + _localizationJson = new cocostudio::JsonLocalizationManager(); + _localizationJson->initLanguageData("configs/en-US.lang.json"); + + _localizationBin = new cocostudio::BinLocalizationManager; + _localizationBin->initLanguageData("configs/ENGLISH.lang.csb"); + + const float BUTTON_WIDTH = 100; + float startPosX = 0; + Size winSize = Director::getInstance()->getVisibleSize(); + + // Create a radio button group + auto radioButtonGroup = RadioButtonGroup::create(); + this->addChild(radioButtonGroup); + + // Create the radio buttons + const int NUMBER_OF_BUTTONS = 3; + startPosX = winSize.width / 2.0f - (NUMBER_OF_BUTTONS - 1) * 0.5 * BUTTON_WIDTH - 30; + std::vector labelTypes = { "English", "Chinese", "Japanese" }; + + for (int i = 0; i < NUMBER_OF_BUTTONS; ++i) + { + RadioButton* radioButton = RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png"); + float posX = startPosX + BUTTON_WIDTH * i; + radioButton->setPosition(Vec2(posX, winSize.height / 2.0f + 70)); + radioButton->setScale(1.2f); + radioButton->addEventListener(CC_CALLBACK_2(LabelLocalizationTest::onChangedRadioButtonSelect, this)); + radioButton->setTag(i); + radioButtonGroup->addRadioButton(radioButton); + this->addChild(radioButton); + + auto label = Label::createWithSystemFont(labelTypes.at(i), "Arial", 20); + label->setPosition(radioButton->getPosition() + Vec2(50, 0)); + this->addChild(label); + } + + _label1 = Label::createWithSystemFont(_localizationJson->getLocalizationString("Text Label"), "Arial", 24); + addChild(_label1, 0); + _label1->setPosition(Vec2(winSize.width / 2, winSize.height * 1 / 3)); + + Label * label = Label::createWithSystemFont("From json data :", "Arial", 24); + label->setAnchorPoint(Vec2(0, 0.5)); + addChild(label, 0); + label->setPosition(Vec2(20, winSize.height * 1 / 3 + 24)); + + _label2 = Label::createWithSystemFont(_localizationBin->getLocalizationString("Text Label"), "Arial", 24); + addChild(_label2, 0); + _label2->setPosition(Vec2(winSize.width / 2, winSize.height * 1 / 2)); + + label = Label::createWithSystemFont("From binary data :", "Arial", 24); + label->setAnchorPoint(Vec2(0, 0.5)); + addChild(label, 0); + label->setPosition(Vec2(20, winSize.height * 1 / 2 + 24)); +} + +std::string LabelLocalizationTest::title() const +{ + return "Localization Test"; +} + +std::string LabelLocalizationTest::subtitle() const +{ + return "Change language selected and see label change"; +} + + +void LabelLocalizationTest::onChangedRadioButtonSelect(RadioButton* radioButton, RadioButton::EventType type) +{ + if (radioButton == nullptr) + { + return; + } + + switch (type) + { + case RadioButton::EventType::SELECTED: + { + switch (radioButton->getTag()) { + case 0: + _localizationJson->initLanguageData("configs/en-US.lang.json"); + _label1->setString(_localizationJson->getLocalizationString("Text Label")); + _localizationBin->initLanguageData("configs/ENGLISH.lang.csb"); + _label2->setString(_localizationJson->getLocalizationString("Text Label")); + break; + case 1: + _localizationJson->initLanguageData("configs/zh-CN.lang.json"); + _label1->setString(_localizationJson->getLocalizationString("Text Label")); + _localizationBin->initLanguageData("configs/CHINESE.lang.csb"); + _label2->setString(_localizationJson->getLocalizationString("Text Label")); + break; + case 2: + _localizationJson->initLanguageData("configs/ja-JP.lang.json"); + _label1->setString(_localizationJson->getLocalizationString("Text Label")); + _localizationBin->initLanguageData("configs/JAPANESE.lang.csb"); + _label2->setString(_localizationJson->getLocalizationString("Text Label")); + break; + default: + break; + } + break; + } + default: + break; + } +} diff --git a/tests/cpp-tests/Classes/LabelTest/LabelTestNew.h b/tests/cpp-tests/Classes/LabelTest/LabelTestNew.h index 5374eea23e..8e9746147e 100644 --- a/tests/cpp-tests/Classes/LabelTest/LabelTestNew.h +++ b/tests/cpp-tests/Classes/LabelTest/LabelTestNew.h @@ -5,6 +5,7 @@ #include "renderer/CCCustomCommand.h" #include "ui/CocosGUI.h" #include "extensions/cocos-ext.h" +#include "editor-support/cocostudio/LocalizationManager.h" DEFINE_TEST_SUITE(NewLabelTests); @@ -842,4 +843,21 @@ public: cocos2d::Label* _label2a; }; +class LabelLocalizationTest : public AtlasDemoNew +{ +public: + CREATE_FUNC(LabelLocalizationTest); + + LabelLocalizationTest(); + void onChangedRadioButtonSelect(cocos2d::ui::RadioButton* radioButton, cocos2d::ui::RadioButton::EventType type); + + virtual std::string title() const override; + virtual std::string subtitle() const override; + + cocos2d::Label* _label1; + cocos2d::Label* _label2; + cocostudio::JsonLocalizationManager* _localizationJson; + cocostudio::BinLocalizationManager* _localizationBin; +}; + #endif diff --git a/tests/cpp-tests/Resources/configs/CHINESE.lang.csb b/tests/cpp-tests/Resources/configs/CHINESE.lang.csb new file mode 100644 index 0000000000000000000000000000000000000000..aa6a4eb12f47c6aa2a6cd6e2920b1433738018ca GIT binary patch literal 84 zcmd;K00A}z4h9w=$p9o77#M+=2Z%XgXA9b&uiXcfW&i-(QVhia literal 0 HcmV?d00001 diff --git a/tests/cpp-tests/Resources/configs/ENGLISH.lang.csb b/tests/cpp-tests/Resources/configs/ENGLISH.lang.csb new file mode 100644 index 0000000000000000000000000000000000000000..25c21b03a67aa4666b1a29cff9ecfc77eb6e9a5d GIT binary patch literal 80 zcmd;K00A}z4h9w=$p9o77#M+=2Z%XbH?4i#wDWOu$K&RekDF&cZe9%(2LNo65@!Ga literal 0 HcmV?d00001 diff --git a/tests/cpp-tests/Resources/configs/en-US.lang.json b/tests/cpp-tests/Resources/configs/en-US.lang.json new file mode 100644 index 0000000000..2ebcecf8ce --- /dev/null +++ b/tests/cpp-tests/Resources/configs/en-US.lang.json @@ -0,0 +1,3 @@ +{ + "Text Label": "Text Label" +} \ No newline at end of file diff --git a/tests/cpp-tests/Resources/configs/ja-JP.lang.json b/tests/cpp-tests/Resources/configs/ja-JP.lang.json new file mode 100644 index 0000000000..3fbdb28493 --- /dev/null +++ b/tests/cpp-tests/Resources/configs/ja-JP.lang.json @@ -0,0 +1,3 @@ +{ + "Text Label": "テキストラベル" +} \ No newline at end of file diff --git a/tests/cpp-tests/Resources/configs/zh-CN.lang.json b/tests/cpp-tests/Resources/configs/zh-CN.lang.json new file mode 100644 index 0000000000..147e2cb637 --- /dev/null +++ b/tests/cpp-tests/Resources/configs/zh-CN.lang.json @@ -0,0 +1,3 @@ +{ + "Text Label": "文本标签" +} \ No newline at end of file