LocalizationMgr.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // LocalizationMgr.cpp
  3. // redream_runtime
  4. //
  5. // Created by ZhengSong on 2022/7/11.
  6. //
  7. #include "LocalizationMgr.h"
  8. NS_CC_BEGIN
  9. static LocalizationMgr* _instance = nullptr;
  10. LocalizationMgr* LocalizationMgr::getInstance()
  11. {
  12. if(_instance == nullptr) {
  13. _instance = new LocalizationMgr();
  14. _instance->init();
  15. }
  16. return _instance;
  17. }
  18. bool LocalizationMgr::init()
  19. {
  20. _currentLanguageCode = Application::getInstance()->getCurrentLanguageFullCode();
  21. return true;
  22. }
  23. std::string LocalizationMgr::getString(const std::string& key, const std::string& path)
  24. {
  25. std::string ret = key;
  26. auto fullPath = FileUtils::getInstance()->fullPathForFilename(path);
  27. if(FileUtils::getInstance()->isFileExist(fullPath))
  28. {
  29. auto plistFile = _getFileFromCache(fullPath);
  30. auto iter = plistFile.find(key);
  31. if(iter != plistFile.end())
  32. {
  33. /*
  34. *从配置文件中根据contryCode寻找多语言,具体逻辑如下
  35. * 当前countryCode->细分语言找不到找主语->找英语->key
  36. */
  37. auto countryCode = getCurrentLanguageCode();
  38. auto dict = iter->second.asValueMap();
  39. if(dict.find(countryCode) != dict.end())
  40. {
  41. ret = dict[countryCode].asString();
  42. }
  43. else
  44. {
  45. std::string defaultCountryCode = "en";
  46. auto findDefaultCountryCode = [=](const std::string& code)->bool
  47. {
  48. if (dict.find(code) != dict.end())
  49. {
  50. return true;
  51. }
  52. return false;
  53. };
  54. if(countryCode.size() > 2) {
  55. //细分语言找不到找主语
  56. std::string shortCode = countryCode.substr(0, 2);
  57. if (dict.find(shortCode) != dict.end())
  58. {
  59. ret = dict[shortCode].asString();
  60. } else if(shortCode != defaultCountryCode){
  61. //如果当前语言找不到就找默认英文
  62. if(findDefaultCountryCode(defaultCountryCode))
  63. {
  64. ret = dict[defaultCountryCode].asString();
  65. }
  66. }
  67. } else if(countryCode != defaultCountryCode){
  68. //如果当前语言找不到就找默认语言
  69. if(findDefaultCountryCode(defaultCountryCode))
  70. {
  71. ret = dict[defaultCountryCode].asString();
  72. }
  73. }
  74. }
  75. }
  76. }
  77. return ret;
  78. }
  79. void LocalizationMgr::setCurrentLanguageCode(const std::string& code)
  80. {
  81. _currentLanguageCode = code;
  82. }
  83. std::string LocalizationMgr::getCurrentLanguageCode() const
  84. {
  85. return _currentLanguageCode;
  86. }
  87. const cocos2d::ValueMap LocalizationMgr::_getFileFromCache(const std::string& path)
  88. {
  89. auto iter = _plistMap.find(path);
  90. if(iter != _plistMap.end())
  91. {
  92. return iter->second;
  93. }
  94. _plistMap[path] = FileUtils::getInstance()->getValueMapFromFile(path);
  95. return _plistMap[path];
  96. }
  97. NS_CC_END