Localisation in Cocos2dx – Multiple Language Strings

Its so simple as that in Cocos2Dx using rapidJson( in-built json lib )

Lets see how to read json files in cocos2dx

std::ostringstream jsonFileName;

jsonFileName << "name.json";

std::string path = FileUtils::getInstance()->fullPathForFilename(fileName);

std::string jsonData = FileUtils::getInstance()->getStringFromFile(path);

 
rapidjson::Document document;

document.Parse<0>(jsonData.c_str());

Now you can get the contents of json file using its KEY

rapidJson::value content = document["your key"];

Its the basic for the localisation as well.

//------------------------------------------------LanguageManager.h

#include <string>

using std::string;

 
#include "cocos2d.h"

USING_NS_CC;

 
#include "../../cocos2d/external/json/document.h"

#include "../../cocos2d/external/json/rapidjson.h"

 
using namespace rapidjson; // library that we use for json parsing

 
class LanguageManager

{

    Document document; // current document with language data

    LanguageManager(); // constructor is private

    static LanguageManager* _instance;

public:

    static LanguageManager* getInstance();

    string getStringForKey(string key);

    static string getString(string key);

};

 

//-----------------------------------------------LanguageManager.cpp

#include "LanguageManager.h"

 
LanguageManager* LanguageManager::_instance = 0;

 
LanguageManager::LanguageManager()

{

    string fileName;

    // detect current language

//    switch(CCApplication::getInstance()->getCurrentLanguage())

    switch(LanguageType::JAPANESE)

    {

        case LanguageType::ENGLISH:

            fileName = "en.json";

            break;

        case LanguageType::RUSSIAN:

            fileName = "ru.json";

            break;

        case  LanguageType::CHINESE:

            fileName = "cn.json";

            break;

        case  LanguageType::FRENCH:

            fileName = "fr.json";

            break;

        case  LanguageType::ITALIAN:

            fileName = "it.json";

            break;

        case  LanguageType::GERMAN:

            fileName = "ge.json";

            break;

        case  LanguageType::SPANISH:

            fileName = "sp.json";

            break;

        case LanguageType:: DUTCH:

            fileName = "du.json";

            break;

        case  LanguageType::KOREAN:

            fileName = "ko.json";

            break;

        case  LanguageType::JAPANESE:

            fileName = "jp.json";

            break;

        case  LanguageType::HUNGARIAN:

            fileName = "hu.json";

            break;

        case  LanguageType::PORTUGUESE:

            fileName = "pt.json";

            break;

        case  LanguageType::ARABIC:

            fileName = "ar.json";

            break;

        case  LanguageType::NORWEGIAN:

            fileName = "nw.json";

            break;

        case  LanguageType::POLISH:

            fileName = "po.json";

            break;

        default:

            CCLOG("Unknown language. Use english");

            fileName = "en.json";

            break;

    };

 
    // below we open, read and parse language data file with rapidjson library

    string clearContent = FileUtils::getInstance()->getStringFromFile(fileName);

 
    document.Parse<0>(clearContent.c_str());

    if(document.HasParseError())

    {

        CCLOG("Language file parsing error!");

        return;

    }

}

 
LanguageManager* LanguageManager::getInstance()

{

    if(!_instance)

        _instance = new LanguageManager();

    return _instance;

}

 
string LanguageManager::getStringForKey(string key)

{

    /* Better Approach

     -------------------------------

 
     std::string ret;

     std::string ss;

     int n, size=100;

     bool b=false;

     va_list marker;

 
     std::string fmt = document[key.c_str()].GetString();

 
     while (!b)

     {

     ss.resize(size);

     va_start(marker, key);

     n = vsnprintf((char*)ss.c_str(), size, fmt.c_str(), marker);

     va_end(marker);

     if ((n>0) && ((b=(n<size))==true)) ss.resize(n); else size*=2;

     }

     ret += ss;

 
     return ret;

     */

 
    //Simple approach

    return document[key.c_str()].GetString();

}

 
string  LanguageManager::getString(string key)

{

    return getInstance()->getStringForKey(key);

}

 
//--------------------------------------------------------------

 
 
Sample Code:

-------------

 
    LanguageManager * man = LanguageManager::getInstance();

 
    std::string info = man->getStringForKey("Hello");

         TextField * txt = some text field reference

          txt->setText(info.c_str());

 
//----------------------------------------------------------------------------------------------------

 
Sample Json file

-------------------

 
{

    "Hello" : "language of ur choice using some translator"

}

 
//----------------------------------------------------------------------------------------------------

 

Cheers

Happy Coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *