NSLocalizedString只有一半的时间可以恢复所引用键的预期值.其他时候我回到NSLocalizedString中指定的密钥名称,并且在每次其他运行时都会一直发生.
目前,我目前只支持英语.
我打电话给:
NSString *someText = NSLocalizedString(@"mystring.keyname", nil);
Run Code Online (Sandbox Code Playgroud)
en.lproj/Localizable.strings的内容:
"mystring.keyname" = "Hello there!";
Run Code Online (Sandbox Code Playgroud)
当它不能正常工作时,someText's价值就是mystring.keyname.
以下是我测试可靠性的方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSLog(@"Strings file: %@", [bundle pathForResource:@"Localizable" ofType:@".strings"]);
NSLog(@"Localizations: %@", [bundle localizations]);
NSLog(@"Local Dict: %@", [bundle localizedInfoDictionary]);
NSLog(@"localizedStringForKey: %@", [bundle localizedStringForKey:@"mystring.keyname"value:@"Wha happened?" table:nil]);
NSLog(@"Localized String: %@", NSLocalizedString(@"mystring.keyname", @"Are you sure you want to start a new game?"));
}
Run Code Online (Sandbox Code Playgroud)
这会在每次运行时打印出预期值:
Run #1:
Local Dict: (null)
localizedStringForKey: Hello there!
Localized String: Hello there! …Run Code Online (Sandbox Code Playgroud) 我有这个格式化的字符串,我正在翻译工作.
英语
"Check out the %1$@ %2$@ in %3$@: %4$@" = "Check out the %1$@ %2$@ in %3$@: %4$@"
德国翻译
"Check out the %1$@ %2$@ in %3$@: %4$@" = "Hör Dir mal %2$@ in %3$@ an: %4$@";
这些传递给一个[NSString stringWithFormat:]电话:
//////////////////////////////////////
// Share Over Twitter
NSString *frmt = NSLocalizedString(@"Check out the %1$@ %2$@ in %3$@: %4$@", @"The default tweet for sharing sounds. Use %1$@ for where the sound type (Sound, mix, playlist) will be, %2$@ for where the audio …Run Code Online (Sandbox Code Playgroud) 我知道很多开发人员就是这样做的:他们开始用英语开发他们的应用程序,而NSLoclaizedString(@"Tap this to do that!", @"Telling what to do...")不是简单地开发@"Tap this to do that!".
然后它们运行genstrings,通过提取所有这些字符串以某种方式创建Localizable.strings文件.凌乱的部分:代码中使用的长文本成为关键.有用.直到有一天你快速进入你的代码并更改英文字符串并忘记本地化并且它作为所有那些Localizable.strings文件的密钥.
所以我倾向于使用"真正的"键,这些键不会与字符串混淆.为了快速测试,我创建了一个本地化为英语和法语的项目.然后我将模拟器语言设置为德语.因为,你知道,如果用户看到钥匙就会非常糟糕TTTDT.
因此,只需使用英语和德语,我就启动了演示应用程序.我得到的是英文Localizable.strings文件中的英文文本.
结论:如果应用程序没有涵盖操作系统语言,NSLocalizedString似乎会回退到英文文件.
Quesion:假设总是有一个Localizable.strings (English)文件,并且文件中的键ID和正确格式化的值.是否存在NSLocalizedString失败然后直接显示密钥的情况?
我正在尝试翻译我的应用程序,当有半个占位符时,我发现很难翻译.我需要找到以下代码:
[textView1 insertText:[NSString stringWithFormat:@"%@ è il %i/%i/%i. Il giorno delle settimana è %@. La Festa è compresa nel calcolo. \n", nomeFesta, giornoFesta, meseFesta, annoFestaModificato, ggSett]];
Run Code Online (Sandbox Code Playgroud)
我把文件localizable.string(英文):
"festaCompresa" = @"%@ is the %i/%i/%i. the day of the week is %@. The holidays is included in the calculation. \n";
Run Code Online (Sandbox Code Playgroud)
然后我编辑了这段代码:
[textView1 insertText:[NSString stringWithFormat:NSLocalizedString(@"festaCompresaW, @""), nomeFesta, giornoFesta, meseFesta, annoFestaModificato, ggSett]];
Run Code Online (Sandbox Code Playgroud)
这是行不通的.
我尝试使用CFBundleDisplayName进行应用名称的本地化.
我根据两种不同的语言制作了本地化的InfoPlist.strings.
在一个字符串文件中写道:
CFBundleDisplayName ="x11111"和另一个:
CFBundleDisplayName ="y22222"
在文件info.plist中,我添加了"应用程序需要iPhone环境"属性,并将布尔值设置为YES.在info.plist中,"Bundle display name"我使用了"x11111".
本地化在模拟器上运行正常,但在设备上进行测试时,它无法正常工作.在设备上,我只能显示我在info.plist中用于"Bundle display name"的名称.
顺便说一句,我使用cocos2d作为项目.
我错过了什么或做错了什么?
我有一个使用本地化的应用程序,我需要再添加一种语言,我想在一个xib文件中显示两个相同名称的不同语言图像,xib文件未本地化,因为我正在本地化图像.可以使用界面构建器完成,而无需编写任何代码吗?
有没有办法在运行单元测试时强制使用特定的语言环境?
例如。始终使用 en_US 或强制无语言环境,以便不加载任何 .lproj 文件。
我的单元测试对设置应用程序(在 iOS 模拟器中)中当前选择的语言很敏感。最好我希望我的单元测试对语言不敏感。
下面是我的单元测试代码,显示了问题
@interface MYGalleryTests : SenTestCase
@end
@implementation MYGalleryTests
-(void)test0 {
// This test doesn't work.
// I get 'No pictures' when locale is en_US
// I get 'Ingen billeder' when locale is da_DK
NSString* actual = [MYGallery emptyMessage];
NSString* expected = @"EMPTY_MESSAGE";
STAssertEqualObjects(actual, expected, nil);
}
@end
@interface MYGallery : NSObject
+(NSString*)emptyMessage;
@end
@implementation MYGallery
+(NSString*)emptyMessage {
return NSLocalizedStringFromTable(@"EMPTY_MESSAGE", @"Gallery", @"Message shown when gallery is empty");
}
@end
en.lproj/Gallery.strings
/* Message …Run Code Online (Sandbox Code Playgroud) 
我目前正在尝试在故事板中创建本地化的accessibilityLabel(我试图避免以编程方式进行).似乎每当我使用Localized String选项时,accessibilityLabels最终都被设置为我提供的本地化字符串键而不是字符串本身.有没有人有这个问题的解决方案?任何帮助将不胜感激.
interface-builder nslocalizedstring ios uiaccessibility ios7
我正在尝试使用Double Length Pseudolanguage来测试其他语言的潜在布局问题.我在方案编辑器中启用了应用程序语言 - >双长度伪语言,但是当我在模拟器中运行应用程序时,没有一个字符串加倍.我在Storyboards中尝试过预览编辑器,字符串保持不变.
仅供参考:我已经将我的应用本地化为两种语言,我有基础语言(英语)和pt-br(葡萄牙语).我在代码中更改了所有硬编码字符串以使用NSLocalizedString.
Apple文档(在Xcode和网页中)都没有任何参数说明。
https://developer.apple.com/documentation/foundation/1418095-nslocalizedstring
作为参考,功能签名为
NSLocalizedString(
_ key : String,
tableName: String? = default, // ??
bundle : Bundle = default,
value : String = default, // ????
comment : String
) -> String
Run Code Online (Sandbox Code Playgroud)
我不清楚这tableName是什么-但更多信息会有所帮助。(这仅仅是字符串文件的文件名吗?)我不知道这value是为了什么。