不使用字符串文件的 cocos2d 游戏本地化
我想为我的游戏添加中文。但我使用的字体文件仅适用于英文。因此我将无法使用额外的字符串文件来添加本地化。另外,我需要显示一些特定的中文图像。所以我认为最好的选择是 if-else 语句。 但当我在网上搜索时,我只发现:
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:@"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:0];
我不知道如何使用它。请给我一个代码示例
,如果我可以在 Constant.h 文件中定义一个布尔值,我将所有常量(例如 #define num_images_per_level 12)放置在其中
,就像:
#define chinese_or_not some_boolean_statement
然后我可以直接使用
if (chinese_or_not) {
//display some image for chinese player only
}
请帮助我(使用代码)。谢谢
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将无法使用 #define 来实现此目的,因为 #define 是一个 预处理器指令,这意味着编译器使用它来决定哪些代码应该被编译,哪些应该被忽略。这使得它不适合任何运行时逻辑(编译后会改变应用程序行为的东西)。
您可以使用单例类来管理本地化,甚至可以在应用程序委托中存储一个标志。
我将查询应用程序委托中的当前语言
applicationdidfinishlaunching
并设置一个标志(使用单例/全局变量)并在整个应用程序中查询:要查询语言,您应该使用 NSLocale 类。
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:For querying the language you should be using the NSLocale class.