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 1/6] 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 From fe917e00fc20168108c1ecf7ea78b39a95f0701a Mon Sep 17 00:00:00 2001 From: "beichen.liu" Date: Mon, 29 Feb 2016 18:00:14 +0800 Subject: [PATCH 2/6] Add language localization support for Button, Text, TextField and TextBMFont. --- .../cocostudio/CSParseBinary_generated.h | 254 ++++++++++-------- .../cocostudio/FlatBuffersSerialize.cpp | 4 +- .../cocostudio/LocalizationManager.cpp | 24 ++ .../cocostudio/LocalizationManager.h | 4 + .../ButtonReader/ButtonReader.cpp | 18 +- .../TextBMFontReader/TextBMFontReader.cpp | 20 +- .../TextFieldReader/TextFieldReader.cpp | 18 +- .../WidgetReader/TextReader/TextReader.cpp | 18 +- 8 files changed, 237 insertions(+), 123 deletions(-) diff --git a/cocos/editor-support/cocostudio/CSParseBinary_generated.h b/cocos/editor-support/cocostudio/CSParseBinary_generated.h index cbc234db89..644e6ac4e4 100644 --- a/cocos/editor-support/cocostudio/CSParseBinary_generated.h +++ b/cocos/editor-support/cocostudio/CSParseBinary_generated.h @@ -1,4 +1,4 @@ -// automatically generated by the FlatBuffers compiler, do not modify +// automatically generated by the FlatBuffers compiler, do not modify #ifndef FLATBUFFERS_GENERATED_CSPARSEBINARY_FLATBUFFERS_H_ #define FLATBUFFERS_GENERATED_CSPARSEBINARY_FLATBUFFERS_H_ @@ -721,21 +721,22 @@ struct ButtonOptions : private flatbuffers::Table { const ResourceData *disabledData() const { return GetPointer(10); } const ResourceData *fontResource() const { return GetPointer(12); } const flatbuffers::String *text() const { return GetPointer(14); } - const flatbuffers::String *fontName() const { return GetPointer(16); } - int32_t fontSize() const { return GetField(18, 0); } - const Color *textColor() const { return GetStruct(20); } - const CapInsets *capInsets() const { return GetStruct(22); } - const FlatSize *scale9Size() const { return GetStruct(24); } - uint8_t scale9Enabled() const { return GetField(26, 0); } - uint8_t displaystate() const { return GetField(28, 1); } - uint8_t outlineEnabled() const { return GetField(30, 0); } - const Color *outlineColor() const { return GetStruct(32); } - int32_t outlineSize() const { return GetField(34, 1); } - uint8_t shadowEnabled() const { return GetField(36, 0); } - const Color *shadowColor() const { return GetStruct(38); } - float shadowOffsetX() const { return GetField(40, 2); } - float shadowOffsetY() const { return GetField(42, -2); } - int32_t shadowBlurRadius() const { return GetField(44, 0); } + uint8_t isLocalized() const { return GetField(16, 0); } + const flatbuffers::String *fontName() const { return GetPointer(18); } + int32_t fontSize() const { return GetField(20, 0); } + const Color *textColor() const { return GetStruct(22); } + const CapInsets *capInsets() const { return GetStruct(24); } + const FlatSize *scale9Size() const { return GetStruct(26); } + uint8_t scale9Enabled() const { return GetField(28, 0); } + uint8_t displaystate() const { return GetField(30, 1); } + uint8_t outlineEnabled() const { return GetField(32, 0); } + const Color *outlineColor() const { return GetStruct(34); } + int32_t outlineSize() const { return GetField(36, 1); } + uint8_t shadowEnabled() const { return GetField(38, 0); } + const Color *shadowColor() const { return GetStruct(40); } + float shadowOffsetX() const { return GetField(42, 2); } + float shadowOffsetY() const { return GetField(44, -2); } + int32_t shadowBlurRadius() const { return GetField(46, 0); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, 4 /* widgetOptions */) && @@ -750,22 +751,23 @@ struct ButtonOptions : private flatbuffers::Table { verifier.VerifyTable(fontResource()) && VerifyField(verifier, 14 /* text */) && verifier.Verify(text()) && - VerifyField(verifier, 16 /* fontName */) && + VerifyField(verifier, 16 /* isLocalized */) && + VerifyField(verifier, 18 /* fontName */) && verifier.Verify(fontName()) && - VerifyField(verifier, 18 /* fontSize */) && - VerifyField(verifier, 20 /* textColor */) && - VerifyField(verifier, 22 /* capInsets */) && - VerifyField(verifier, 24 /* scale9Size */) && - VerifyField(verifier, 26 /* scale9Enabled */) && - VerifyField(verifier, 28 /* displaystate */) && - VerifyField(verifier, 30 /* outlineEnabled */) && - VerifyField(verifier, 32 /* outlineColor */) && - VerifyField(verifier, 34 /* outlineSize */) && - VerifyField(verifier, 36 /* shadowEnabled */) && - VerifyField(verifier, 38 /* shadowColor */) && - VerifyField(verifier, 40 /* shadowOffsetX */) && - VerifyField(verifier, 42 /* shadowOffsetY */) && - VerifyField(verifier, 44 /* shadowBlurRadius */) && + VerifyField(verifier, 20 /* fontSize */) && + VerifyField(verifier, 22 /* textColor */) && + VerifyField(verifier, 24 /* capInsets */) && + VerifyField(verifier, 26 /* scale9Size */) && + VerifyField(verifier, 28 /* scale9Enabled */) && + VerifyField(verifier, 30 /* displaystate */) && + VerifyField(verifier, 32 /* outlineEnabled */) && + VerifyField(verifier, 34 /* outlineColor */) && + VerifyField(verifier, 36 /* outlineSize */) && + VerifyField(verifier, 38 /* shadowEnabled */) && + VerifyField(verifier, 40 /* shadowColor */) && + VerifyField(verifier, 42 /* shadowOffsetX */) && + VerifyField(verifier, 44 /* shadowOffsetY */) && + VerifyField(verifier, 46 /* shadowBlurRadius */) && verifier.EndTable(); } }; @@ -779,25 +781,26 @@ struct ButtonOptionsBuilder { void add_disabledData(flatbuffers::Offset disabledData) { fbb_.AddOffset(10, disabledData); } void add_fontResource(flatbuffers::Offset fontResource) { fbb_.AddOffset(12, fontResource); } void add_text(flatbuffers::Offset text) { fbb_.AddOffset(14, text); } - void add_fontName(flatbuffers::Offset fontName) { fbb_.AddOffset(16, fontName); } - void add_fontSize(int32_t fontSize) { fbb_.AddElement(18, fontSize, 0); } - void add_textColor(const Color *textColor) { fbb_.AddStruct(20, textColor); } - void add_capInsets(const CapInsets *capInsets) { fbb_.AddStruct(22, capInsets); } - void add_scale9Size(const FlatSize *scale9Size) { fbb_.AddStruct(24, scale9Size); } - void add_scale9Enabled(uint8_t scale9Enabled) { fbb_.AddElement(26, scale9Enabled, 0); } - void add_displaystate(uint8_t displaystate) { fbb_.AddElement(28, displaystate, 1); } - void add_outlineEnabled(uint8_t outlineEnabled) { fbb_.AddElement(30, outlineEnabled, 0); } - void add_outlineColor(const Color *outlineColor) { fbb_.AddStruct(32, outlineColor); } - void add_outlineSize(int32_t outlineSize) { fbb_.AddElement(34, outlineSize, 1); } - void add_shadowEnabled(uint8_t shadowEnabled) { fbb_.AddElement(36, shadowEnabled, 0); } - void add_shadowColor(const Color *shadowColor) { fbb_.AddStruct(38, shadowColor); } - void add_shadowOffsetX(float shadowOffsetX) { fbb_.AddElement(40, shadowOffsetX, 2); } - void add_shadowOffsetY(float shadowOffsetY) { fbb_.AddElement(42, shadowOffsetY, -2); } - void add_shadowBlurRadius(int32_t shadowBlurRadius) { fbb_.AddElement(44, shadowBlurRadius, 0); } + void add_isLocalized(uint8_t isLocalized) { fbb_.AddElement(16, isLocalized, 0); } + void add_fontName(flatbuffers::Offset fontName) { fbb_.AddOffset(18, fontName); } + void add_fontSize(int32_t fontSize) { fbb_.AddElement(20, fontSize, 0); } + void add_textColor(const Color *textColor) { fbb_.AddStruct(22, textColor); } + void add_capInsets(const CapInsets *capInsets) { fbb_.AddStruct(24, capInsets); } + void add_scale9Size(const FlatSize *scale9Size) { fbb_.AddStruct(26, scale9Size); } + void add_scale9Enabled(uint8_t scale9Enabled) { fbb_.AddElement(28, scale9Enabled, 0); } + void add_displaystate(uint8_t displaystate) { fbb_.AddElement(30, displaystate, 1); } + void add_outlineEnabled(uint8_t outlineEnabled) { fbb_.AddElement(32, outlineEnabled, 0); } + void add_outlineColor(const Color *outlineColor) { fbb_.AddStruct(34, outlineColor); } + void add_outlineSize(int32_t outlineSize) { fbb_.AddElement(36, outlineSize, 1); } + void add_shadowEnabled(uint8_t shadowEnabled) { fbb_.AddElement(38, shadowEnabled, 0); } + void add_shadowColor(const Color *shadowColor) { fbb_.AddStruct(40, shadowColor); } + void add_shadowOffsetX(float shadowOffsetX) { fbb_.AddElement(42, shadowOffsetX, 2); } + void add_shadowOffsetY(float shadowOffsetY) { fbb_.AddElement(44, shadowOffsetY, -2); } + void add_shadowBlurRadius(int32_t shadowBlurRadius) { fbb_.AddElement(46, shadowBlurRadius, 0); } ButtonOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } ButtonOptionsBuilder &operator=(const ButtonOptionsBuilder &); flatbuffers::Offset Finish() { - auto o = flatbuffers::Offset(fbb_.EndTable(start_, 21)); + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 22)); return o; } }; @@ -809,6 +812,7 @@ inline flatbuffers::Offset CreateButtonOptions(flatbuffers::FlatB flatbuffers::Offset disabledData = 0, flatbuffers::Offset fontResource = 0, flatbuffers::Offset text = 0, + uint8_t isLocalized = 0, flatbuffers::Offset fontName = 0, int32_t fontSize = 0, const Color *textColor = 0, @@ -846,6 +850,7 @@ inline flatbuffers::Offset CreateButtonOptions(flatbuffers::FlatB builder_.add_outlineEnabled(outlineEnabled); builder_.add_displaystate(displaystate); builder_.add_scale9Enabled(scale9Enabled); + builder_.add_isLocalized(isLocalized); return builder_.Finish(); } @@ -1029,6 +1034,7 @@ struct TextBMFontOptions : private flatbuffers::Table { const WidgetOptions *widgetOptions() const { return GetPointer(4); } const ResourceData *fileNameData() const { return GetPointer(6); } const flatbuffers::String *text() const { return GetPointer(8); } + uint8_t isLocalized() const { return GetField(10, 0); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, 4 /* widgetOptions */) && @@ -1037,6 +1043,7 @@ struct TextBMFontOptions : private flatbuffers::Table { verifier.VerifyTable(fileNameData()) && VerifyField(verifier, 8 /* text */) && verifier.Verify(text()) && + VerifyField(verifier, 10 /* isLocalized */) && verifier.EndTable(); } }; @@ -1047,10 +1054,11 @@ struct TextBMFontOptionsBuilder { void add_widgetOptions(flatbuffers::Offset widgetOptions) { fbb_.AddOffset(4, widgetOptions); } void add_fileNameData(flatbuffers::Offset fileNameData) { fbb_.AddOffset(6, fileNameData); } void add_text(flatbuffers::Offset text) { fbb_.AddOffset(8, text); } + void add_isLocalized(uint8_t isLocalized) { fbb_.AddElement(10, isLocalized, 0); } TextBMFontOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } TextBMFontOptionsBuilder &operator=(const TextBMFontOptionsBuilder &); flatbuffers::Offset Finish() { - auto o = flatbuffers::Offset(fbb_.EndTable(start_, 3)); + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 4)); return o; } }; @@ -1058,11 +1066,13 @@ struct TextBMFontOptionsBuilder { inline flatbuffers::Offset CreateTextBMFontOptions(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset widgetOptions = 0, flatbuffers::Offset fileNameData = 0, - flatbuffers::Offset text = 0) { + flatbuffers::Offset text = 0, + uint8_t isLocalized = 0) { TextBMFontOptionsBuilder builder_(_fbb); builder_.add_text(text); builder_.add_fileNameData(fileNameData); builder_.add_widgetOptions(widgetOptions); + builder_.add_isLocalized(isLocalized); return builder_.Finish(); } @@ -1072,20 +1082,21 @@ struct TextOptions : private flatbuffers::Table { const flatbuffers::String *fontName() const { return GetPointer(8); } int32_t fontSize() const { return GetField(10, 0); } const flatbuffers::String *text() const { return GetPointer(12); } - int32_t areaWidth() const { return GetField(14, 0); } - int32_t areaHeight() const { return GetField(16, 0); } - int32_t hAlignment() const { return GetField(18, 0); } - int32_t vAlignment() const { return GetField(20, 0); } - uint8_t touchScaleEnable() const { return GetField(22, 0); } - uint8_t isCustomSize() const { return GetField(24, 0); } - uint8_t outlineEnabled() const { return GetField(26, 0); } - const Color *outlineColor() const { return GetStruct(28); } - int32_t outlineSize() const { return GetField(30, 1); } - uint8_t shadowEnabled() const { return GetField(32, 0); } - const Color *shadowColor() const { return GetStruct(34); } - float shadowOffsetX() const { return GetField(36, 2); } - float shadowOffsetY() const { return GetField(38, -2); } - int32_t shadowBlurRadius() const { return GetField(40, 0); } + uint8_t isLocalized() const { return GetField(14, 0); } + int32_t areaWidth() const { return GetField(16, 0); } + int32_t areaHeight() const { return GetField(18, 0); } + int32_t hAlignment() const { return GetField(20, 0); } + int32_t vAlignment() const { return GetField(22, 0); } + uint8_t touchScaleEnable() const { return GetField(24, 0); } + uint8_t isCustomSize() const { return GetField(26, 0); } + uint8_t outlineEnabled() const { return GetField(28, 0); } + const Color *outlineColor() const { return GetStruct(30); } + int32_t outlineSize() const { return GetField(32, 1); } + uint8_t shadowEnabled() const { return GetField(34, 0); } + const Color *shadowColor() const { return GetStruct(36); } + float shadowOffsetX() const { return GetField(38, 2); } + float shadowOffsetY() const { return GetField(40, -2); } + int32_t shadowBlurRadius() const { return GetField(42, 0); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, 4 /* widgetOptions */) && @@ -1097,20 +1108,21 @@ struct TextOptions : private flatbuffers::Table { VerifyField(verifier, 10 /* fontSize */) && VerifyField(verifier, 12 /* text */) && verifier.Verify(text()) && - VerifyField(verifier, 14 /* areaWidth */) && - VerifyField(verifier, 16 /* areaHeight */) && - VerifyField(verifier, 18 /* hAlignment */) && - VerifyField(verifier, 20 /* vAlignment */) && - VerifyField(verifier, 22 /* touchScaleEnable */) && - VerifyField(verifier, 24 /* isCustomSize */) && - VerifyField(verifier, 26 /* outlineEnabled */) && - VerifyField(verifier, 28 /* outlineColor */) && - VerifyField(verifier, 30 /* outlineSize */) && - VerifyField(verifier, 32 /* shadowEnabled */) && - VerifyField(verifier, 34 /* shadowColor */) && - VerifyField(verifier, 36 /* shadowOffsetX */) && - VerifyField(verifier, 38 /* shadowOffsetY */) && - VerifyField(verifier, 40 /* shadowBlurRadius */) && + VerifyField(verifier, 14 /* isLocalized */) && + VerifyField(verifier, 16 /* areaWidth */) && + VerifyField(verifier, 18 /* areaHeight */) && + VerifyField(verifier, 20 /* hAlignment */) && + VerifyField(verifier, 22 /* vAlignment */) && + VerifyField(verifier, 24 /* touchScaleEnable */) && + VerifyField(verifier, 26 /* isCustomSize */) && + VerifyField(verifier, 28 /* outlineEnabled */) && + VerifyField(verifier, 30 /* outlineColor */) && + VerifyField(verifier, 32 /* outlineSize */) && + VerifyField(verifier, 34 /* shadowEnabled */) && + VerifyField(verifier, 36 /* shadowColor */) && + VerifyField(verifier, 38 /* shadowOffsetX */) && + VerifyField(verifier, 40 /* shadowOffsetY */) && + VerifyField(verifier, 42 /* shadowBlurRadius */) && verifier.EndTable(); } }; @@ -1123,24 +1135,25 @@ struct TextOptionsBuilder { void add_fontName(flatbuffers::Offset fontName) { fbb_.AddOffset(8, fontName); } void add_fontSize(int32_t fontSize) { fbb_.AddElement(10, fontSize, 0); } void add_text(flatbuffers::Offset text) { fbb_.AddOffset(12, text); } - void add_areaWidth(int32_t areaWidth) { fbb_.AddElement(14, areaWidth, 0); } - void add_areaHeight(int32_t areaHeight) { fbb_.AddElement(16, areaHeight, 0); } - void add_hAlignment(int32_t hAlignment) { fbb_.AddElement(18, hAlignment, 0); } - void add_vAlignment(int32_t vAlignment) { fbb_.AddElement(20, vAlignment, 0); } - void add_touchScaleEnable(uint8_t touchScaleEnable) { fbb_.AddElement(22, touchScaleEnable, 0); } - void add_isCustomSize(uint8_t isCustomSize) { fbb_.AddElement(24, isCustomSize, 0); } - void add_outlineEnabled(uint8_t outlineEnabled) { fbb_.AddElement(26, outlineEnabled, 0); } - void add_outlineColor(const Color *outlineColor) { fbb_.AddStruct(28, outlineColor); } - void add_outlineSize(int32_t outlineSize) { fbb_.AddElement(30, outlineSize, 1); } - void add_shadowEnabled(uint8_t shadowEnabled) { fbb_.AddElement(32, shadowEnabled, 0); } - void add_shadowColor(const Color *shadowColor) { fbb_.AddStruct(34, shadowColor); } - void add_shadowOffsetX(float shadowOffsetX) { fbb_.AddElement(36, shadowOffsetX, 2); } - void add_shadowOffsetY(float shadowOffsetY) { fbb_.AddElement(38, shadowOffsetY, -2); } - void add_shadowBlurRadius(int32_t shadowBlurRadius) { fbb_.AddElement(40, shadowBlurRadius, 0); } + void add_isLocalized(uint8_t isLocalized) { fbb_.AddElement(14, isLocalized, 0); } + void add_areaWidth(int32_t areaWidth) { fbb_.AddElement(16, areaWidth, 0); } + void add_areaHeight(int32_t areaHeight) { fbb_.AddElement(18, areaHeight, 0); } + void add_hAlignment(int32_t hAlignment) { fbb_.AddElement(20, hAlignment, 0); } + void add_vAlignment(int32_t vAlignment) { fbb_.AddElement(22, vAlignment, 0); } + void add_touchScaleEnable(uint8_t touchScaleEnable) { fbb_.AddElement(24, touchScaleEnable, 0); } + void add_isCustomSize(uint8_t isCustomSize) { fbb_.AddElement(26, isCustomSize, 0); } + void add_outlineEnabled(uint8_t outlineEnabled) { fbb_.AddElement(28, outlineEnabled, 0); } + void add_outlineColor(const Color *outlineColor) { fbb_.AddStruct(30, outlineColor); } + void add_outlineSize(int32_t outlineSize) { fbb_.AddElement(32, outlineSize, 1); } + void add_shadowEnabled(uint8_t shadowEnabled) { fbb_.AddElement(34, shadowEnabled, 0); } + void add_shadowColor(const Color *shadowColor) { fbb_.AddStruct(36, shadowColor); } + void add_shadowOffsetX(float shadowOffsetX) { fbb_.AddElement(38, shadowOffsetX, 2); } + void add_shadowOffsetY(float shadowOffsetY) { fbb_.AddElement(40, shadowOffsetY, -2); } + void add_shadowBlurRadius(int32_t shadowBlurRadius) { fbb_.AddElement(42, shadowBlurRadius, 0); } TextOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } TextOptionsBuilder &operator=(const TextOptionsBuilder &); flatbuffers::Offset Finish() { - auto o = flatbuffers::Offset(fbb_.EndTable(start_, 19)); + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 20)); return o; } }; @@ -1151,6 +1164,7 @@ inline flatbuffers::Offset CreateTextOptions(flatbuffers::FlatBuffe flatbuffers::Offset fontName = 0, int32_t fontSize = 0, flatbuffers::Offset text = 0, + uint8_t isLocalized = 0, int32_t areaWidth = 0, int32_t areaHeight = 0, int32_t hAlignment = 0, @@ -1185,6 +1199,7 @@ inline flatbuffers::Offset CreateTextOptions(flatbuffers::FlatBuffe builder_.add_outlineEnabled(outlineEnabled); builder_.add_isCustomSize(isCustomSize); builder_.add_touchScaleEnable(touchScaleEnable); + builder_.add_isLocalized(isLocalized); return builder_.Finish(); } @@ -1194,14 +1209,15 @@ struct TextFieldOptions : private flatbuffers::Table { const flatbuffers::String *fontName() const { return GetPointer(8); } int32_t fontSize() const { return GetField(10, 0); } const flatbuffers::String *text() const { return GetPointer(12); } - const flatbuffers::String *placeHolder() const { return GetPointer(14); } - uint8_t passwordEnabled() const { return GetField(16, 0); } - const flatbuffers::String *passwordStyleText() const { return GetPointer(18); } - uint8_t maxLengthEnabled() const { return GetField(20, 0); } - int32_t maxLength() const { return GetField(22, 0); } - int32_t areaWidth() const { return GetField(24, 0); } - int32_t areaHeight() const { return GetField(26, 0); } - uint8_t isCustomSize() const { return GetField(28, 0); } + uint8_t isLocalized() const { return GetField(14, 0); } + const flatbuffers::String *placeHolder() const { return GetPointer(16); } + uint8_t passwordEnabled() const { return GetField(18, 0); } + const flatbuffers::String *passwordStyleText() const { return GetPointer(20); } + uint8_t maxLengthEnabled() const { return GetField(22, 0); } + int32_t maxLength() const { return GetField(24, 0); } + int32_t areaWidth() const { return GetField(26, 0); } + int32_t areaHeight() const { return GetField(28, 0); } + uint8_t isCustomSize() const { return GetField(30, 0); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, 4 /* widgetOptions */) && @@ -1213,16 +1229,17 @@ struct TextFieldOptions : private flatbuffers::Table { VerifyField(verifier, 10 /* fontSize */) && VerifyField(verifier, 12 /* text */) && verifier.Verify(text()) && - VerifyField(verifier, 14 /* placeHolder */) && + VerifyField(verifier, 14 /* isLocalized */) && + VerifyField(verifier, 16 /* placeHolder */) && verifier.Verify(placeHolder()) && - VerifyField(verifier, 16 /* passwordEnabled */) && - VerifyField(verifier, 18 /* passwordStyleText */) && + VerifyField(verifier, 18 /* passwordEnabled */) && + VerifyField(verifier, 20 /* passwordStyleText */) && verifier.Verify(passwordStyleText()) && - VerifyField(verifier, 20 /* maxLengthEnabled */) && - VerifyField(verifier, 22 /* maxLength */) && - VerifyField(verifier, 24 /* areaWidth */) && - VerifyField(verifier, 26 /* areaHeight */) && - VerifyField(verifier, 28 /* isCustomSize */) && + VerifyField(verifier, 22 /* maxLengthEnabled */) && + VerifyField(verifier, 24 /* maxLength */) && + VerifyField(verifier, 26 /* areaWidth */) && + VerifyField(verifier, 28 /* areaHeight */) && + VerifyField(verifier, 30 /* isCustomSize */) && verifier.EndTable(); } }; @@ -1235,18 +1252,19 @@ struct TextFieldOptionsBuilder { void add_fontName(flatbuffers::Offset fontName) { fbb_.AddOffset(8, fontName); } void add_fontSize(int32_t fontSize) { fbb_.AddElement(10, fontSize, 0); } void add_text(flatbuffers::Offset text) { fbb_.AddOffset(12, text); } - void add_placeHolder(flatbuffers::Offset placeHolder) { fbb_.AddOffset(14, placeHolder); } - void add_passwordEnabled(uint8_t passwordEnabled) { fbb_.AddElement(16, passwordEnabled, 0); } - void add_passwordStyleText(flatbuffers::Offset passwordStyleText) { fbb_.AddOffset(18, passwordStyleText); } - void add_maxLengthEnabled(uint8_t maxLengthEnabled) { fbb_.AddElement(20, maxLengthEnabled, 0); } - void add_maxLength(int32_t maxLength) { fbb_.AddElement(22, maxLength, 0); } - void add_areaWidth(int32_t areaWidth) { fbb_.AddElement(24, areaWidth, 0); } - void add_areaHeight(int32_t areaHeight) { fbb_.AddElement(26, areaHeight, 0); } - void add_isCustomSize(uint8_t isCustomSize) { fbb_.AddElement(28, isCustomSize, 0); } + void add_isLocalized(uint8_t isLocalized) { fbb_.AddElement(14, isLocalized, 0); } + void add_placeHolder(flatbuffers::Offset placeHolder) { fbb_.AddOffset(16, placeHolder); } + void add_passwordEnabled(uint8_t passwordEnabled) { fbb_.AddElement(18, passwordEnabled, 0); } + void add_passwordStyleText(flatbuffers::Offset passwordStyleText) { fbb_.AddOffset(20, passwordStyleText); } + void add_maxLengthEnabled(uint8_t maxLengthEnabled) { fbb_.AddElement(22, maxLengthEnabled, 0); } + void add_maxLength(int32_t maxLength) { fbb_.AddElement(24, maxLength, 0); } + void add_areaWidth(int32_t areaWidth) { fbb_.AddElement(26, areaWidth, 0); } + void add_areaHeight(int32_t areaHeight) { fbb_.AddElement(28, areaHeight, 0); } + void add_isCustomSize(uint8_t isCustomSize) { fbb_.AddElement(30, isCustomSize, 0); } TextFieldOptionsBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } TextFieldOptionsBuilder &operator=(const TextFieldOptionsBuilder &); flatbuffers::Offset Finish() { - auto o = flatbuffers::Offset(fbb_.EndTable(start_, 13)); + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 14)); return o; } }; @@ -1257,6 +1275,7 @@ inline flatbuffers::Offset CreateTextFieldOptions(flatbuffers: flatbuffers::Offset fontName = 0, int32_t fontSize = 0, flatbuffers::Offset text = 0, + uint8_t isLocalized = 0, flatbuffers::Offset placeHolder = 0, uint8_t passwordEnabled = 0, flatbuffers::Offset passwordStyleText = 0, @@ -1279,6 +1298,7 @@ inline flatbuffers::Offset CreateTextFieldOptions(flatbuffers: builder_.add_isCustomSize(isCustomSize); builder_.add_maxLengthEnabled(maxLengthEnabled); builder_.add_passwordEnabled(passwordEnabled); + builder_.add_isLocalized(isLocalized); return builder_.Finish(); } diff --git a/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp b/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp index 34aee128aa..a09fd0b44d 100644 --- a/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp +++ b/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp @@ -1615,8 +1615,10 @@ std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFileForLanguageData else if (strcmp(languageName.c_str(), childElement->Name()) == 0) { const char* langText = childElement->GetText(); - if (langText) + if (langText && langText[0] != '\0') text = langText; + else + text = key; hasTextReaded = true; } diff --git a/cocos/editor-support/cocostudio/LocalizationManager.cpp b/cocos/editor-support/cocostudio/LocalizationManager.cpp index 9980fe8503..f2dd985be6 100644 --- a/cocos/editor-support/cocostudio/LocalizationManager.cpp +++ b/cocos/editor-support/cocostudio/LocalizationManager.cpp @@ -6,6 +6,18 @@ using namespace cocostudio; using namespace cocos2d; +static JsonLocalizationManager* _sharedJsonLocalizationManager = nullptr; + +JsonLocalizationManager* JsonLocalizationManager::getInstance() +{ + if (!_sharedJsonLocalizationManager) + { + _sharedJsonLocalizationManager = new (std::nothrow) JsonLocalizationManager(); + } + + return _sharedJsonLocalizationManager; +} + JsonLocalizationManager::JsonLocalizationManager() :languageData(nullptr) { @@ -46,6 +58,18 @@ std::string JsonLocalizationManager::getLocalizationString(std::string key) return result; } +static BinLocalizationManager* _sharedBinLocalizationManager = nullptr; + +BinLocalizationManager* BinLocalizationManager::getInstance() +{ + if (!_sharedBinLocalizationManager) + { + _sharedBinLocalizationManager = new (std::nothrow) BinLocalizationManager(); + } + + return _sharedBinLocalizationManager; +} + BinLocalizationManager::BinLocalizationManager() { diff --git a/cocos/editor-support/cocostudio/LocalizationManager.h b/cocos/editor-support/cocostudio/LocalizationManager.h index 055d291783..17f3ed3b7d 100644 --- a/cocos/editor-support/cocostudio/LocalizationManager.h +++ b/cocos/editor-support/cocostudio/LocalizationManager.h @@ -26,6 +26,8 @@ namespace cocostudio { class CC_STUDIO_DLL JsonLocalizationManager : ILocalizationManager { public: + static JsonLocalizationManager* getInstance(); + JsonLocalizationManager(); ~JsonLocalizationManager(); @@ -50,6 +52,8 @@ namespace cocostudio { class CC_STUDIO_DLL BinLocalizationManager : ILocalizationManager { public: + static BinLocalizationManager* getInstance(); + BinLocalizationManager(); ~BinLocalizationManager(); diff --git a/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp index 6d1a41ebe7..e0af24bda2 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp @@ -6,6 +6,7 @@ #include "cocostudio/CocoLoader.h" #include "cocostudio/CSParseBinary_generated.h" #include "cocostudio/FlatBuffersSerialize.h" +#include "cocostudio/LocalizationManager.h" #include "tinyxml2.h" #include "flatbuffers/flatbuffers.h" @@ -253,6 +254,7 @@ namespace cocostudio bool scale9Enabled = false; Rect capInsets; std::string text = ""; + bool isLocalized = false; int fontSize = 14; std::string fontName = ""; cocos2d::Size scale9Size; @@ -316,6 +318,10 @@ namespace cocostudio { text = value; } + else if (name == "IsLocalized") + { + isLocalized = (value == "True") ? true : false; + } else if (name == "FontSize") { fontSize = atoi(value.c_str()); @@ -624,6 +630,7 @@ namespace cocostudio builder->CreateString(fontResourcePlistFile), fontResourceResourceType), builder->CreateString(text), + isLocalized, builder->CreateString(fontName), fontSize, &f_textColor, @@ -824,7 +831,16 @@ namespace cocostudio } std::string titleText = options->text()->c_str(); - button->setTitleText(titleText); + bool isLocalized = options->isLocalized() != 0; + if (isLocalized) + { + BinLocalizationManager* localizer = BinLocalizationManager::getInstance(); + button->setTitleText(localizer->getLocalizationString(titleText)); + } + else + { + button->setTitleText(titleText); + } auto textColor = options->textColor(); Color3B titleColor(textColor->r(), textColor->g(), textColor->b()); diff --git a/cocos/editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.cpp index 1bf03ec09d..b83091c5c8 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.cpp @@ -6,6 +6,7 @@ #include "ui/UITextBMFont.h" #include "cocostudio/CocoLoader.h" #include "cocostudio/CSParseBinary_generated.h" +#include "cocostudio/LocalizationManager.h" #include "tinyxml2.h" #include "flatbuffers/flatbuffers.h" @@ -124,6 +125,7 @@ namespace cocostudio auto widgetOptions = *(Offset*)(&temp); std::string text = "Fnt Text Label"; + bool isLocalized = false; std::string path = ""; std::string plistFlie = ""; @@ -140,6 +142,10 @@ namespace cocostudio { text = value; } + else if (name == "IsLocalized") + { + isLocalized = (value == "True") ? true : false; + } attribute = attribute->Next(); } @@ -185,7 +191,8 @@ namespace cocostudio builder->CreateString(path), builder->CreateString(plistFlie), resourceType), - builder->CreateString(text)); + builder->CreateString(text), + isLocalized); return *(Offset*)(&options); } @@ -230,7 +237,16 @@ namespace cocostudio } std::string text = options->text()->c_str(); - labelBMFont->setString(text); + bool isLocalized = options->isLocalized() != 0; + if (isLocalized) + { + BinLocalizationManager* localizer = BinLocalizationManager::getInstance(); + labelBMFont->setString(localizer->getLocalizationString(text)); + } + else + { + labelBMFont->setString(text); + } auto widgetReader = WidgetReader::getInstance(); widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions()); diff --git a/cocos/editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.cpp index 95293bd2a5..8d1401963d 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.cpp @@ -5,6 +5,7 @@ #include "ui/UITextField.h" #include "cocostudio/CocoLoader.h" #include "cocostudio/CSParseBinary_generated.h" +#include "cocostudio/LocalizationManager.h" #include "tinyxml2.h" #include "flatbuffers/flatbuffers.h" @@ -164,6 +165,7 @@ namespace cocostudio std::string fontName = ""; int fontSize = 20; std::string text = ""; + bool isLocalized = false; std::string placeHolder = "Text Field"; bool passwordEnabled = false; std::string passwordStyleText = "*"; @@ -189,6 +191,10 @@ namespace cocostudio { text = value; } + else if (name == "IsLocalized") + { + isLocalized = (value == "True") ? true : false; + } else if (name == "FontSize") { fontSize = atoi(value.c_str()); @@ -266,6 +272,7 @@ namespace cocostudio builder->CreateString(fontName), fontSize, builder->CreateString(text), + isLocalized, builder->CreateString(placeHolder), passwordEnabled, builder->CreateString(passwordStyleText), @@ -288,7 +295,16 @@ namespace cocostudio textField->setPlaceHolder(placeholder); std::string text = options->text()->c_str(); - textField->setString(text); + bool isLocalized = options->isLocalized() != 0; + if (isLocalized) + { + BinLocalizationManager* localizer = BinLocalizationManager::getInstance(); + textField->setString(localizer->getLocalizationString(text)); + } + else + { + textField->setString(text); + } int fontSize = options->fontSize(); textField->setFontSize(fontSize); diff --git a/cocos/editor-support/cocostudio/WidgetReader/TextReader/TextReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/TextReader/TextReader.cpp index 962ad2b66f..8082278e96 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/TextReader/TextReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/TextReader/TextReader.cpp @@ -5,6 +5,7 @@ #include "ui/UIText.h" #include "cocostudio/CocoLoader.h" #include "cocostudio/CSParseBinary_generated.h" +#include "cocostudio/LocalizationManager.h" #include "tinyxml2.h" #include "flatbuffers/flatbuffers.h" @@ -160,6 +161,7 @@ namespace cocostudio std::string fontName = ""; int fontSize = 20; std::string text = "Text Label"; + bool isLocalized = false; int areaWidth = 0; int areaHeight = 0; int h_alignment = 0; @@ -191,6 +193,10 @@ namespace cocostudio { text = value; } + else if (name == "IsLocalized") + { + isLocalized = (value == "True") ? true : false; + } else if (name == "FontSize") { fontSize = atoi(value.c_str()); @@ -374,6 +380,7 @@ namespace cocostudio builder->CreateString(fontName), fontSize, builder->CreateString(text), + isLocalized, areaWidth, areaHeight, h_alignment, @@ -450,7 +457,16 @@ namespace cocostudio } std::string text = options->text()->c_str(); - label->setString(text); + bool isLocalized = options->isLocalized() != 0; + if (isLocalized) + { + BinLocalizationManager* localizer = BinLocalizationManager::getInstance(); + label->setString(localizer->getLocalizationString(text)); + } + else + { + label->setString(text); + } // Save node color before set widget properties auto oldColor = node->getColor(); From 4e4558e5098a5c4e29d342eec775a544c908f2d2 Mon Sep 17 00:00:00 2001 From: "beichen.liu" Date: Tue, 1 Mar 2016 14:27:12 +0800 Subject: [PATCH 3/6] Modify the interface of localization manager. --- .../cocostudio/LocalizationManager.cpp | 20 +++++++++++++++++-- .../cocostudio/LocalizationManager.h | 19 ++++++++++++++++-- .../ButtonReader/ButtonReader.cpp | 4 ++-- .../TextBMFontReader/TextBMFontReader.cpp | 4 ++-- .../TextFieldReader/TextFieldReader.cpp | 4 ++-- .../WidgetReader/TextReader/TextReader.cpp | 4 ++-- 6 files changed, 43 insertions(+), 12 deletions(-) diff --git a/cocos/editor-support/cocostudio/LocalizationManager.cpp b/cocos/editor-support/cocostudio/LocalizationManager.cpp index f2dd985be6..b6df13e898 100644 --- a/cocos/editor-support/cocostudio/LocalizationManager.cpp +++ b/cocos/editor-support/cocostudio/LocalizationManager.cpp @@ -8,7 +8,7 @@ using namespace cocos2d; static JsonLocalizationManager* _sharedJsonLocalizationManager = nullptr; -JsonLocalizationManager* JsonLocalizationManager::getInstance() +ILocalizationManager* JsonLocalizationManager::getInstance() { if (!_sharedJsonLocalizationManager) { @@ -60,7 +60,7 @@ std::string JsonLocalizationManager::getLocalizationString(std::string key) static BinLocalizationManager* _sharedBinLocalizationManager = nullptr; -BinLocalizationManager* BinLocalizationManager::getInstance() +ILocalizationManager* BinLocalizationManager::getInstance() { if (!_sharedBinLocalizationManager) { @@ -121,3 +121,19 @@ std::string BinLocalizationManager::getLocalizationString(std::string key) return result; } + +static ILocalizationManager* _sharedLocalizationManager = nullptr; + +ILocalizationManager* LocalizationHelper::getCurrentManager() +{ + if (!_sharedLocalizationManager) + { + _sharedLocalizationManager = BinLocalizationManager::getInstance(); + } + return _sharedLocalizationManager; +} + +void LocalizationHelper::setCurrentManager(ILocalizationManager* manager) +{ + _sharedLocalizationManager = manager; +} \ No newline at end of file diff --git a/cocos/editor-support/cocostudio/LocalizationManager.h b/cocos/editor-support/cocostudio/LocalizationManager.h index 17f3ed3b7d..a06326a9d8 100644 --- a/cocos/editor-support/cocostudio/LocalizationManager.h +++ b/cocos/editor-support/cocostudio/LocalizationManager.h @@ -26,7 +26,7 @@ namespace cocostudio { class CC_STUDIO_DLL JsonLocalizationManager : ILocalizationManager { public: - static JsonLocalizationManager* getInstance(); + static ILocalizationManager* getInstance(); JsonLocalizationManager(); ~JsonLocalizationManager(); @@ -52,7 +52,7 @@ namespace cocostudio { class CC_STUDIO_DLL BinLocalizationManager : ILocalizationManager { public: - static BinLocalizationManager* getInstance(); + static ILocalizationManager* getInstance(); BinLocalizationManager(); ~BinLocalizationManager(); @@ -73,6 +73,21 @@ namespace cocostudio { protected: std::unordered_map languageData; }; + + class CC_STUDIO_DLL LocalizationHelper + { + public: + /* Get current localization manager. + * @return If the manager has been set, return current localization manager, + * otherwise return the singleton instance of BinLocalizationManager. + */ + static ILocalizationManager* getCurrentManager(); + + /* Set current localization manager. + * @param manager Instance of current localization manager. + */ + static void setCurrentManager(ILocalizationManager* manager); + }; } #endif //__LOCALLIZATION_MANAGER_H__ \ No newline at end of file diff --git a/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp index e0af24bda2..c3289cba02 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp @@ -834,8 +834,8 @@ namespace cocostudio bool isLocalized = options->isLocalized() != 0; if (isLocalized) { - BinLocalizationManager* localizer = BinLocalizationManager::getInstance(); - button->setTitleText(localizer->getLocalizationString(titleText)); + ILocalizationManager* lm = LocalizationHelper::getCurrentManager(); + button->setTitleText(lm->getLocalizationString(titleText)); } else { diff --git a/cocos/editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.cpp index b83091c5c8..8207e91b38 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.cpp @@ -240,8 +240,8 @@ namespace cocostudio bool isLocalized = options->isLocalized() != 0; if (isLocalized) { - BinLocalizationManager* localizer = BinLocalizationManager::getInstance(); - labelBMFont->setString(localizer->getLocalizationString(text)); + ILocalizationManager* lm = LocalizationHelper::getCurrentManager(); + labelBMFont->setString(lm->getLocalizationString(text)); } else { diff --git a/cocos/editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.cpp index 8d1401963d..cf7ffd3a3b 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.cpp @@ -298,8 +298,8 @@ namespace cocostudio bool isLocalized = options->isLocalized() != 0; if (isLocalized) { - BinLocalizationManager* localizer = BinLocalizationManager::getInstance(); - textField->setString(localizer->getLocalizationString(text)); + ILocalizationManager* lm = LocalizationHelper::getCurrentManager(); + textField->setString(lm->getLocalizationString(text)); } else { diff --git a/cocos/editor-support/cocostudio/WidgetReader/TextReader/TextReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/TextReader/TextReader.cpp index 8082278e96..e0def35086 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/TextReader/TextReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/TextReader/TextReader.cpp @@ -460,8 +460,8 @@ namespace cocostudio bool isLocalized = options->isLocalized() != 0; if (isLocalized) { - BinLocalizationManager* localizer = BinLocalizationManager::getInstance(); - label->setString(localizer->getLocalizationString(text)); + ILocalizationManager* lm = LocalizationHelper::getCurrentManager(); + label->setString(lm->getLocalizationString(text)); } else { From 91581754a4483a1e3e856bdd385f905f66691051 Mon Sep 17 00:00:00 2001 From: "beichen.liu" Date: Tue, 1 Mar 2016 14:34:13 +0800 Subject: [PATCH 4/6] Add introductions of localization manager in test. --- tests/cpp-empty-test/Classes/AppDelegate.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/cpp-empty-test/Classes/AppDelegate.cpp b/tests/cpp-empty-test/Classes/AppDelegate.cpp index 740b147211..cd8d66dfc6 100644 --- a/tests/cpp-empty-test/Classes/AppDelegate.cpp +++ b/tests/cpp-empty-test/Classes/AppDelegate.cpp @@ -6,6 +6,8 @@ #include "HelloWorldScene.h" #include "AppMacros.h" +#include "editor-support/cocostudio/LocalizationManager.h" + USING_NS_CC; using namespace std; @@ -71,7 +73,23 @@ bool AppDelegate::applicationDidFinishLaunching() { // set searching path FileUtils::getInstance()->setSearchPaths(searchPath); - + + // Uncomment follow block to use localize manager to set localize strings + + // If you want to load json localize data, use follow block + /* + cocostudio::JsonLocalizationManager * lm = cocostudio::JsonLocalizationManager::getInstance(); + lm->initLanguageData("your localize file name.lang.json"); + cocostudio::LocalizationHelper::setCurrentManager(lm); + */ + + // If you want to load binary localize data, use follow block + /* + cocostudio::BinLocalizationManager * lm = cocostudio::BinLocalizationManager::getInstance(); + lm->initLanguageData("your localize file name.lang.csb"); + cocostudio::LocalizationHelper::setCurrentManager(lm); + */ + // turn on display FPS director->setDisplayStats(true); From 05411a44f5a1984d7d7942654730a0317ad302ec Mon Sep 17 00:00:00 2001 From: "beichen.liu" Date: Thu, 3 Mar 2016 13:41:32 +0800 Subject: [PATCH 5/6] Add comment. --- tests/cpp-empty-test/Classes/AppDelegate.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/cpp-empty-test/Classes/AppDelegate.cpp b/tests/cpp-empty-test/Classes/AppDelegate.cpp index cd8d66dfc6..107b81a258 100644 --- a/tests/cpp-empty-test/Classes/AppDelegate.cpp +++ b/tests/cpp-empty-test/Classes/AppDelegate.cpp @@ -6,7 +6,8 @@ #include "HelloWorldScene.h" #include "AppMacros.h" -#include "editor-support/cocostudio/LocalizationManager.h" +//Uncomment the following line to use localize manager +//#include "editor-support/cocostudio/LocalizationManager.h" USING_NS_CC; using namespace std; From 0489fd76e81651e3f79d7abfe07ec0af728af785 Mon Sep 17 00:00:00 2001 From: "beichen.liu" Date: Thu, 3 Mar 2016 15:49:50 +0800 Subject: [PATCH 6/6] 1.Change the interface in LocalizationManager. 2.Add new args of localization in simulator. --- .../cocostudio/LocalizationManager.cpp | 44 ++++++++++++++----- .../cocostudio/LocalizationManager.h | 31 ++++++++----- tests/cpp-empty-test/Classes/AppDelegate.cpp | 8 ++-- .../lib/ProjectConfig/ProjectConfig.cpp | 22 ++++++++++ .../lib/ProjectConfig/ProjectConfig.h | 2 + 5 files changed, 83 insertions(+), 24 deletions(-) diff --git a/cocos/editor-support/cocostudio/LocalizationManager.cpp b/cocos/editor-support/cocostudio/LocalizationManager.cpp index b6df13e898..2ac9ed2d85 100644 --- a/cocos/editor-support/cocostudio/LocalizationManager.cpp +++ b/cocos/editor-support/cocostudio/LocalizationManager.cpp @@ -18,6 +18,15 @@ ILocalizationManager* JsonLocalizationManager::getInstance() return _sharedJsonLocalizationManager; } +void JsonLocalizationManager::destroyInstance() +{ + if (_sharedJsonLocalizationManager != nullptr) + { + delete _sharedJsonLocalizationManager; + _sharedJsonLocalizationManager = nullptr; + } +} + JsonLocalizationManager::JsonLocalizationManager() :languageData(nullptr) { @@ -58,6 +67,8 @@ std::string JsonLocalizationManager::getLocalizationString(std::string key) return result; } + + static BinLocalizationManager* _sharedBinLocalizationManager = nullptr; ILocalizationManager* BinLocalizationManager::getInstance() @@ -70,14 +81,21 @@ ILocalizationManager* BinLocalizationManager::getInstance() return _sharedBinLocalizationManager; } +void BinLocalizationManager::destroyInstance() +{ + if (_sharedBinLocalizationManager != nullptr) + { + delete _sharedBinLocalizationManager; + _sharedBinLocalizationManager = nullptr; + } +} + BinLocalizationManager::BinLocalizationManager() { - } BinLocalizationManager::~BinLocalizationManager() { - } bool BinLocalizationManager::initLanguageData(std::string file) @@ -122,18 +140,24 @@ std::string BinLocalizationManager::getLocalizationString(std::string key) return result; } -static ILocalizationManager* _sharedLocalizationManager = nullptr; + + +static bool isCurrentBinManager = true; ILocalizationManager* LocalizationHelper::getCurrentManager() { - if (!_sharedLocalizationManager) - { - _sharedLocalizationManager = BinLocalizationManager::getInstance(); - } - return _sharedLocalizationManager; + if (isCurrentBinManager) + return BinLocalizationManager::getInstance(); + else + return JsonLocalizationManager::getInstance(); } -void LocalizationHelper::setCurrentManager(ILocalizationManager* manager) +void LocalizationHelper::setCurrentManager(bool isBinary) { - _sharedLocalizationManager = manager; + isCurrentBinManager = isBinary; +} + +bool LocalizationHelper::isBinManager() +{ + return isCurrentBinManager; } \ No newline at end of file diff --git a/cocos/editor-support/cocostudio/LocalizationManager.h b/cocos/editor-support/cocostudio/LocalizationManager.h index a06326a9d8..7cd254092b 100644 --- a/cocos/editor-support/cocostudio/LocalizationManager.h +++ b/cocos/editor-support/cocostudio/LocalizationManager.h @@ -27,9 +27,7 @@ namespace cocostudio { { public: static ILocalizationManager* getInstance(); - - JsonLocalizationManager(); - ~JsonLocalizationManager(); + static void destroyInstance(); public: /* Init manager with special localize json data file. @@ -45,6 +43,10 @@ namespace cocostudio { */ virtual std::string getLocalizationString(std::string key); + protected: + JsonLocalizationManager(); + ~JsonLocalizationManager(); + protected: rapidjson::Document * languageData; }; @@ -53,9 +55,7 @@ namespace cocostudio { { public: static ILocalizationManager* getInstance(); - - BinLocalizationManager(); - ~BinLocalizationManager(); + static void destroyInstance(); /* Init manager with special localize binary data file. * @param file Name of localize file. @@ -70,6 +70,10 @@ namespace cocostudio { */ virtual std::string getLocalizationString(std::string key); + protected: + BinLocalizationManager(); + ~BinLocalizationManager(); + protected: std::unordered_map languageData; }; @@ -78,15 +82,22 @@ namespace cocostudio { { public: /* Get current localization manager. - * @return If the manager has been set, return current localization manager, - * otherwise return the singleton instance of BinLocalizationManager. + * @return The instance of current localization manager. + * If the manager hasn't been set, the default manager will be BinLocalizationManager. */ static ILocalizationManager* getCurrentManager(); /* Set current localization manager. - * @param manager Instance of current localization manager. + * @param isBinary If the param is true, current manager will be set to BinLocalizationManager. + * If the param is false, current manager will be set to JsonLocalizationManager. */ - static void setCurrentManager(ILocalizationManager* manager); + static void setCurrentManager(bool isBinary); + + /* Get the type of current localization manager. + * @return If current manager is BinLocalizationManager, return true. + * Otherwise return false, that means current manager is JsonLocalizationManager. + */ + static bool isBinManager(); }; } diff --git a/tests/cpp-empty-test/Classes/AppDelegate.cpp b/tests/cpp-empty-test/Classes/AppDelegate.cpp index 107b81a258..ac3672d881 100644 --- a/tests/cpp-empty-test/Classes/AppDelegate.cpp +++ b/tests/cpp-empty-test/Classes/AppDelegate.cpp @@ -79,16 +79,16 @@ bool AppDelegate::applicationDidFinishLaunching() { // If you want to load json localize data, use follow block /* - cocostudio::JsonLocalizationManager * lm = cocostudio::JsonLocalizationManager::getInstance(); + cocostudio::LocalizationHelper::setCurrentManager(false); + cocostudio::ILocalizationManager * lm = cocostudio::LocalizationHelper::getCurrentManager(); lm->initLanguageData("your localize file name.lang.json"); - cocostudio::LocalizationHelper::setCurrentManager(lm); */ // If you want to load binary localize data, use follow block /* - cocostudio::BinLocalizationManager * lm = cocostudio::BinLocalizationManager::getInstance(); + cocostudio::LocalizationHelper::setCurrentManager(true); + cocostudio::ILocalizationManager * lm = cocostudio::LocalizationHelper::getCurrentManager(); lm->initLanguageData("your localize file name.lang.csb"); - cocostudio::LocalizationHelper::setCurrentManager(lm); */ // turn on display FPS diff --git a/tools/simulator/libsimulator/lib/ProjectConfig/ProjectConfig.cpp b/tools/simulator/libsimulator/lib/ProjectConfig/ProjectConfig.cpp index 0e75e2bfde..4338d68695 100644 --- a/tools/simulator/libsimulator/lib/ProjectConfig/ProjectConfig.cpp +++ b/tools/simulator/libsimulator/lib/ProjectConfig/ProjectConfig.cpp @@ -409,6 +409,12 @@ void ProjectConfig::parseCommandLine(const vector &args) vector pathes = split((*it), ';'); setSearchPath(pathes); } + else if (arg.compare("-language-data-path") == 0) + { + ++it; + if (it == args.end()) break; + setLanguageDataPath(*it); + } ++it; } @@ -615,6 +621,22 @@ const vector &ProjectConfig::getSearchPath() const return _searchPath; } +void ProjectConfig::setLanguageDataPath(const std::string &filePath) +{ + bool isBinary = true; + string jsonExtension = ".json"; + int exLength = jsonExtension.length(); + if (filePath.length() >= exLength && + (0 == filePath.compare(filePath.length() - exLength, exLength, jsonExtension))) + { + isBinary = false; + } + + cocostudio::LocalizationHelper::setCurrentManager(isBinary); + cocostudio::ILocalizationManager* lm = cocostudio::LocalizationHelper::getCurrentManager(); + lm->initLanguageData(filePath); +} + bool ProjectConfig::isAppMenu() const { return _isAppMenu; diff --git a/tools/simulator/libsimulator/lib/ProjectConfig/ProjectConfig.h b/tools/simulator/libsimulator/lib/ProjectConfig/ProjectConfig.h index 75244ab4cb..4ad9b86ef7 100644 --- a/tools/simulator/libsimulator/lib/ProjectConfig/ProjectConfig.h +++ b/tools/simulator/libsimulator/lib/ProjectConfig/ProjectConfig.h @@ -104,6 +104,7 @@ public: const std::string &getBindAddress() const; void setSearchPath(const vector &args); const vector &getSearchPath() const; + void setLanguageDataPath(const std::string &filePath); bool isAppMenu() const; bool isResizeWindow() const; @@ -133,6 +134,7 @@ private: int _fileUploadPort; string _bindAddress; vector _searchPath; + string _languageDataPath; void normalize(); string replaceProjectDirToMacro(const string &path) const;