SHARE
TWEET

Localized.cpp

a guest Mar 25th, 2014 436 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  Localized.cpp
  3. //  astroGems
  4. //
  5. //  Created by Alex Gievsky on 30.10.13.
  6. //
  7. //
  8.  
  9. #include "Localized.h"
  10. #include "Shared.h"
  11. #include "rapidjson/document.h"
  12. #include "GameConfig.h"
  13.  
  14. Localized *Localized::__sharedInstance = nullptr;
  15.  
  16. static LanguageType AvailableLanguages[] = {
  17.     LanguageType::ENGLISH,
  18.     LanguageType::RUSSIAN,
  19.     LanguageType::GERMAN
  20. };
  21.  
  22. Localized::~Localized() {
  23.     //
  24. }
  25.  
  26. Localized::Localized() {
  27.     //
  28. }
  29.  
  30. void Localized::load() {
  31.     if(__sharedInstance == nullptr) {
  32.         __sharedInstance = new Localized();
  33.     } else {
  34.         return;
  35.     }
  36.    
  37.     unsigned char *t = nullptr;
  38.     unsigned long stringsDataSize = 0;
  39.     t = (FileUtils::getInstance()->getFileData("strings.json", "r", &stringsDataSize));
  40.        
  41.     char *stringsData = new char[stringsDataSize + 1];
  42.     memcpy(stringsData, t, stringsDataSize);
  43.     stringsData[stringsDataSize] = NULL;
  44.    
  45.     delete[] t;
  46.     t = NULL;
  47.    
  48.     rapidjson::Document stringsDoc;
  49.     stringsDoc.Parse<0>(stringsData);
  50.    
  51.     const auto &stringsMap = stringsDoc["strings"];
  52.     if(stringsMap.IsObject()) {
  53.         for(auto it = stringsMap.MemberBegin(); it != stringsMap.MemberEnd(); ++it) {
  54.             string key = it->name.GetString();
  55.             string value = it->value.GetString();
  56.            
  57.             __sharedInstance->strings.insert(make_pair(key, value));
  58.         }
  59.     }
  60.    
  61.     delete[] stringsData;
  62. }
  63.  
  64. void Localized::purge() {
  65.     delete __sharedInstance;
  66.     __sharedInstance = nullptr;
  67. }
  68.  
  69. string Localized::getString(const string &key) {
  70.     if(__sharedInstance == nullptr) {
  71.         Localized::load();
  72.     }
  73.    
  74.     auto stringIt = __sharedInstance->strings.find(key);
  75.    
  76.     if(stringIt != __sharedInstance->strings.end()) {
  77.         return stringIt->second;
  78.     } else {
  79.         CCLOG("String not found for key: %s", key.c_str());
  80.     }
  81.    
  82.     return kLocalizedStringNotFound;
  83. }
  84.  
  85. bool Localized::isLanguageSupported(LanguageType language) {
  86.     for(LanguageType l: AvailableLanguages) {
  87.         if(l == language) {
  88.             return true;
  89.         }
  90.     }
  91.    
  92.     return false;
  93. }
  94.  
  95. string Localized::languageShortNameForType(cocos2d::LanguageType type) {
  96.     string languagePrefix = "en";
  97.     switch(type) {
  98.         case LanguageType::ENGLISH:
  99.             languagePrefix = "en"; break;
  100.             //        case LanguageType::CHINESE:
  101.             //            languagePrefix = "zh"; break;
  102.             //        case LanguageType::FRENCH:
  103.             //            languagePrefix = "fr"; break;
  104.             //        case LanguageType::ITALIAN:
  105.             //            languagePrefix = "it"; break;
  106.         case LanguageType::GERMAN:
  107.                         languagePrefix = "de"; break;
  108.             //        case LanguageType::SPANISH:
  109.             //            languagePrefix = "es"; break;
  110.         case LanguageType::RUSSIAN:
  111.             languagePrefix = "ru"; break;
  112.             //        case LanguageType::KOREAN:
  113.             //            languagePrefix = "ko"; break;
  114.             //        case LanguageType::JAPANESE:
  115.             //            languagePrefix = "ja"; break;
  116.             //        case LanguageType::HUNGARIAN:
  117.             //            languagePrefix = "hu"; break;
  118.             //        case LanguageType::PORTUGUESE:
  119.             //            languagePrefix = "pt"; break;
  120.             //        case LanguageType::ARABIC:
  121.             //            languagePrefix = "ar"; break;
  122.             //        case LanguageType::NORWEGIAN:
  123.             //            languagePrefix = "nb"; break;
  124.             //        case LanguageType::POLISH:
  125.             //            languagePrefix = "pl"; break;
  126.         default:
  127.             break;
  128.     }
  129.    
  130.     return languagePrefix;
  131.     //return "en";
  132. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top