开发者

cocos2d game localization without using string file

开发者 https://www.devze.com 2023-04-12 21:57 出处:网络
i want to add chinese language for my game. but the font file i used is only for english. thus i won\'t be able to use additional string file to add localization. also, i need to display some specific

i want to add chinese language for my game. but the font file i used is only for english. thus i won't be able to use additional string file to add localization. also, i need to display some specific image for chinese language. so i think the best choice is an if-else statement. but when i search online, i only found: 开发者_如何学Python

NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:@"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:0];

i don't know how to use it. please give me a code example

also, it would be great if i can define a boolean value in my Constant.h file, where i put all my constants (e.g. #define num_images_per_level 12)

like:

#define chinese_or_not some_boolean_statement

then i can directly use

if (chinese_or_not) {
    //display some image for chinese player only
}

please help me (using code). thank you


You won't be able to use a #define for this purpose since a #define is a preprocessor directive, which means it is used by the compiler to decide which code should be compiled and which should be ignored. This makes it unsuitable for any runtime logic (things that will change the application behaviour after it has been compiled).

You could use a singleton class to manage your localization, or even store a flag in your app delegate.

I would query the current language in your app delegates applicationdidfinishlaunching and set a flag (using a singleton/global variable) and query that throughout the application:

if ([[LocalizationManager instance] getLanguage] == kChinese) {...}

For querying the language you should be using the NSLocale class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号