简体   繁体   English

在本地化 Objective-c 中连接两个字符串

[英]Concatenating Two strings in Localization Objective-c

My Previous Code without Localization.我以前没有本地化的代码。 It worked perfect.它工作完美。

case LOGIN_LOGOUT: ((Cell*)cell).lbl.text = [self isLoggedIn] ?   
[NSString stringWithFormat:@"Logout %@", email]   
:NSLocalizedString(@"Login", @"Message");
 break;  

But when I implement Localization in Logout the email will not show.但是当我在注销时实施本地化时,电子邮件不会显示。

case LOGIN_LOGOUT: ((Cell*)cell).lbl.text = [self isLoggedIn] ?   
[NSString stringWithFormat:NSLocalizedString(@"Logout", @"Message") ,"%@",   
email] :NSLocalizedString(@"Login", @"Message");
break;  

I know I am missing some basics in stringWithFormat but can anyone offer some guidance to me?我知道我在stringWithFormat缺少一些基础知识,但谁能为我提供一些指导?

You are looking up the localisation of "Logout".您正在查找“注销”的本地化。 You are using that as a format string.您将其用作格式字符串。 That's not likely to work.那不太可能奏效。 Don't make statements that are too complex, it makes it impossible to debug.不要做太复杂的语句,会导致无法调试。

I'd write我会写

case LOGIN_LOGOUT: {
    NSString* labelText; 
    if ([self isLoggedIn]) {
        NSString* formatString = NSLocalizedString(@"Logout", @"Message");
        labelText = [NSString stringWithFormat:formatString, "%@", email];
    } else {
        labelText = NSLocalizedString(@"Login", @"Message");
    }

    ((Cell*)cell).lbl.text = labelText;
    break;
}

And now you can actually debug that whole mess.现在您实际上可以调试整个混乱。 The stringWithFormat parameters look very, very dodgy. stringWithFormat 参数看起来非常非常狡猾。

Let's assume, that you have .strings file and it contains entry named "Logout" .假设您有 .strings 文件,其中包含名为"Logout" 的条目。 You have:你有:

[NSString stringWithFormat:NSLocalizedString(@"Logout", @"Message") ,"%@", email]

here you try to load format string via NSLocalizedString and use it with NSString .在这里,您尝试通过NSLocalizedString加载格式字符串并将其与NSString一起使用。 That means that you have to put correct format string into your .strings file, so, if currently you have:这意味着您必须将正确的格式字符串放入 .strings 文件中,因此,如果您目前有:

"Logout" = "Logout";

In order to make it just like before localization, you need:为了使它像本地化之前一样,您需要:

"Logout" = "Logout %@";

If you don't have a .strings file or don't have entry named "Logout" , NSLocalizedString will return the key, ie如果您没有 .strings 文件或没有名为"Logout" 的条目, NSLocalizedString将返回密钥,即

NSLocalizedString(@"key", @"comment") // returns "key"

That means, that your NSLocalizedString(@"Logout", @"Message") may return "Logout" if NSLocalizedString can't find correct entry in your .strings file.这意味着,如果NSLocalizedString在您的 .strings 文件中找不到正确的条目,您的NSLocalizedString(@"Logout", @"Message")可能会返回“Logout”

There are more things that may go wrong, if you want some deeper insides on that, I have written great article on the whole topic: Understanding iOS internationalization .还有更多可能出错的地方,如果你想了解更深入的内容,我写了一篇关于整个主题的很棒的文章: 了解 iOS 国际化

Also I'd suggest to use +localizedStringWithFormat: instead of just plain +stringWithFormat: , because the former uses current locale.另外我建议使用+localizedStringWithFormat:而不是简单的+stringWithFormat: ,因为前者使用当前语言环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM