CCLocalizedString localize your app string like NSLocalizedString,easy to use!

CCLocalizedString feature:

  1. easy to use
  2. support for line break

Have been tested on IOS and android.

thank you for Lance Gray he help me to improve some code.

note: language file must save as UTF-8 .

download sample project from URL below.
http://url.cn/5PHmPR

1 Like

Good job!
But why not publish it on github?

sorry, i don’t know how to publish it on github.
sample project is too big for uploading.
here is CCLocalizedString files and test localized files of english and chinese.

useage:

create a helloworld project of cocos2d-x 2.0.1 above.

change
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);
to
CCLabelTTF* pLabel = CCLabelTTF::create(CCLocalizedString("HELLO", "HELLO"), "Thonburi", 34);

I have been trying to use this in my current project (iOS & Android). I have it working in iOS but having a hard time getting android to compile properly. I’m pretty sure it has to do with the location of the file and the Android.mk file. The link to the full project is dead so I am justt guessing as to what you changed and did. Here is what I have done so far

1: Place CCLocalizedString.h/cpp inside cocos2dx/platform/
2: Added: #include “platform/CCLocalizedString.h” — to cocos2dx/include/cocos2d.h
3: Added: platform/CCLocalizedString.cpp — to cocos2dx/Android.mk

When I try to test from eclipse I get the following errors:

Description Resource Path Location Type
fatal error: CCString.h: No such file or directory line 13, external location: /Users/Mike/xxx/xxx/cocos2dx/platform/CCLocalizedString.cpp C/C*+ Problem
Description Resource Path Location Type
make: * Error 1 C/C*+ Problem

Can you possibly shed light on the proper location and what I should be adjusting to make it compile correctly for Android. Or perhaps you can post another copy of the full source example to see it al together.

you doesn’t need to place CCLocalizedString.h/cpp inside cocos2dx/platform/.
just place it to your project folder(android) as normal, you can contact me by email: 181399700@qq.com .hope you will solve it ASAP.

Thanks for the reply. I ended up getting it working. I left it in platform since it seems to make sense there for now. I just had to add a directory in front of the include to: #include “cocoa/CCString.h” and it worked along with the other things I did above.

This class is absolutely awesome, it should be incorporated into the actual cocos2dx feature set. I always used NSLocalizedString in pure iOS work so nice to have it available in CC2DX now.

Very nice.
It doesn’t return a good value the first time it is invoked (at least, not in Win32), as string which “c_str()” is returned, is destroyed when function returns.

        if (resultStr.length() > 0){
            return (string(resultStr)).c_str();
        }
        return mKey;

Can be fixed easily removing resultStr, and replacing the above with:

        if (!localizedStrings.empty() )
        {
           return CCLocalizedString( mKey, mComment );
        }
        else
        {
           return mKey;
        }
1 Like

Unable to open the link :http://url.cn/5PHmPR
Who can post a valid url, thank you very much.

http://git.oschina.net/sue602/TestCCLocalizedString.git

Im using moFileReader realy great. I use PO-files like in web :). Super and light https://code.google.com/p/mofilereader/

EASY!

http://git.oschina.net/sue602/TestCCLocalizedString.git

+1 on PO / MO files. This requires minimum changes to existing code.
eg. pseudocode.
CCLabelTTF::create("text",...);
to something like
CCLabelTTF::create(t("text"),...);

where “t” is something like:

string t(const char* text) { return moFileReaderSingleton::GetInstance().Lookup(text); }
You can load a language .mo file like this:

moFileReaderSingleton::GetInstance().ReadFile(CCFileUtils::sharedFileUtils()->fullPathForFilename("ro.mo").c_str());

Use POEdit to extract all t("") strings from your code and save the .mo file.

Awesome :slight_smile:

gelldur wrote:

Im using moFileReader realy great. I use PO-files like in web :). Super and light https://code.google.com/p/mofilereader/

EASY!

Is this works with android? I cannot open file. It is working on windows but no on android. Did you have any problems with that?

My solution works on Android, iOS,Linux and WP8 it is really simple it uses only your resources

@gelldur I am step closer to use it but I cannot understand one thing. Android assets are no just “folders” and “files” and as far I know:
moFileReader::ReadFile(const char* filename) cannot open file from there, even I do that:
moFileLib::moFileReaderSingleton::GetInstance().ReadFile(FileUtils::getInstance()->fullPathForFilename("dict/en.mo").c_str());

The problem is later here:
std::ifstream stream(filename, std::ios_base::binary | std::ios_base::in); in moFileReader::readFile.

So how exactly I have to read this mo file from assets? I know that NDK has AssetsManager which is used by cocos FileUtils, but this is going to nowhere. I end with cocos2d::Data with binary data of my mo file. How can I get moFileLib working? Please give me some thoughts.

May someone share with some code which can handle reading file with dictionary (but on Android where dictionary is in assets folder like other resources). I cannot achieve this due to fact that I cannot have stream to assets item. Please give some ideas.

I had the same issue and found the solution here: http://gameit.ro/2014/04/internationalization-with-cocos2d-x-supporting-multiple-languages/

It’s a moFileLib a bit modified (with the method ParseFile), parsing the file located in assets without opening an ifstream.

Hope it helps!

1 Like

Thanks sue602, the class is absolutely awesome! and I wonder why this hasn’t been a part of cocos2d-x platform codes.

So I took some liberty of forking the code and make it compatible with cocos2d-x 3.x versions, here’s a github link:

1 Like

Great job!
Thanks sue602 and marcwjj for the awesome class. I also hope it will become an official part in cocos2dx.

@walzer: It would be great if you consider to add Vietnamese as an supported language into cocos2dx?

Thanks a lot!