如何定义一个函数宏来检查多个本地化字符串文件之间的键? [英] How to define a function macro to check key between multiple localized string files?

查看:144
本文介绍了如何定义一个函数宏来检查多个本地化字符串文件之间的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Localizable.strings 文件为我的项目的i18n,和一个lib使用 KYLocalizable.strings 。 / p>

我已经考虑过从 KYLocalizable.strings 中创建 Localizable.strings code>,但它不能据我所知。因此,我想定义一个函数宏,就像SDK做的:

  #define NSLocalizedString(key,comment)\ 
[[NSBundle mainBundle] localizedStringForKey:(key)value:@table:nil]
#define NSLocalizedStringFromTable(key,tbl,comment)\
[[NSBundle mainBundle] localizedStringForKey key)value:@table:(tbl)]

伪代码:

  #define CustomLocalizedString(key,comment)\ 
//如果Key在Localizable.strings中存在
//使用它
[[NSBundle mainBundle] localizedStringForKey:(key)value:@table:nil]
// else
//在KYLocalizable.strings中搜索
[[NSBundle mainBundle ] localizedStringForKey:(key)value:@table:@KYLocalizable]

只需在我的项目中使用 CustomLocalizedString(< key>,< comment>)

但是如何检查 Localizable.strings 还是不是?



提前感谢!

解决方案

如果键不存在,您将收到的字符串将是键本身。
所以,只要你假设你永远不会使用键作为本地化的字符串,你可以测试NSLocalizableString是否返回你的密钥。


I have a Localizable.strings file for my project's i18n, and a lib uses KYLocalizable.strings.

I have considered to make Localizable.strings "subclass" from KYLocalizable.strings, but it cannot as far as I know. So instead, I want to define a function macro like what SDK does:

#define NSLocalizedString(key, comment) \
    [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]
#define NSLocalizedStringFromTable(key, tbl, comment) \
    [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:(tbl)]

Pseudo code:

#define CustomLocalizedString(key, comment) \
    // if key exists in Localizable.strings
    //   use it
    [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]
    // else
    //   search it in KYLocalizable.strings
    [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:@"KYLocalizable"]

so I can just use CustomLocalizedString(<key>, <comment>) in my project.
But how to check whether the key exists in Localizable.strings or not?

Thanks in advance!!

解决方案

If the key doesn't exist, the string you will receive will be the key itself. So as long as you suppose you will never use the key as a localized string, you can test if NSLocalizableString returned you the key or not.

这篇关于如何定义一个函数宏来检查多个本地化字符串文件之间的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆