高级本地化,省略XCode中的参数
-
05-10-2019 - |
题
我有这个格式化的字符串,我正在使用翻译人员的工作。
英语
"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 name will be, %3$@ for the app name, and %3$@ for where the sound link will be.");
NSString *urlString = [NSString stringWithFormat:@"sounds/%@", SoundSoundID(audio)];
NSString *url = ([audio audioType] == UAAudioTypeSound ? UrlFor(urlString) : APP_SHORTLINK);
NSString *msg = [NSString stringWithFormat:
frmt,
[[Audio titleForAudioType:[audio audioType]] lowercaseString],
[NSString stringWithFormat:@"\"%@\"", AudioName(audio)],
APP_NAME,
url];
returnString = msg;
带有所需和实际结果:
英语
desired: "Check out the sound "This Sound Name" in My App Name: link_to_sound" actual: "Check out the sound "This Sound Name" in My App Name: link_to_sound"
德语
desired: "Hör Dir mal "This Sound Name" in My App Name an: link_to_sound" actual: "Hör Dir mal sound in "This Sound Name" an: My App Name"
问题问题在于我的假设是,通过使用编号变量 -[NSString stringWithFormat:]
, ,我可以做这样的事情, %1$@
变量被完全省略。如果注意到,格式字符串的德语翻译不会使用第一个参数(%1$@
)除了它(“声音”)之外,仍然出现在输出字符串中。
我究竟做错了什么?
解决方案
这不是错误。编号的论点不是C标准的一部分,而是IEEE STD 1003.1的一部分,其中说以下(强调我的):
该格式可以包含编号的参数转换规范(即“%n $”和“ * m $”),也可以包含未编号的参数转换规范(即,%和 *),但不是两者兼而有之。唯一的例外是%%可以与“%n $”表格混合。格式字符串中混合编号和未编号的参数规范的结果是不确定的。 当使用编号的参数规范时,指定nth参数要求在格式字符串中指定所有从第一个到(n-1)TH的主要参数。
其他提示
对我来说看起来像是一个错误。我认为你应该提起一个错误.
CFString的格式引擎独立于 fprintf
这样可能会有一些差异。例如,
printf("a %3$s\n", "b", "c", "d"); // prints "a d"
NSLog(@"a %3$s\n", "b", "c", "d"); // prints "a b"
您需要提供所有以前的指定符,因为不需要解决参数的宽度,例如
printf("%2$llx %1$llx\n", 1LL, 2LL); // prints "2 1"
printf("%2$llx\n", 1LL, 2LL); // prints "200000000" !!
NSLog(@"%2$llx %1$llx\n", 1LL, 2LL); // prints "2 1"
NSLog(@"%2$llx\n", 1LL, 2LL); // prints "1"
iPhone操作系统 printf
跳过1个缺少指定符上的4个字节,而CFString的格式化器跳过0字节。
解决方案是:
重新排列您的索引,例如
"Check out the %4$@ %1$@ in %2$@: %3$@" "Hör Dir mal %1$@ in %2$@ an: %3$@";
或者
使用格式
[@"%1$10p%2$10p%3$10p%4$10p" stringByAppendingString:frmt]
强制使用所有参数,然后用
-substringFromIndex:
, , 或者将所有OBJC对象转换为c字符串(
char*
)和使用snprintf
.- 编写自己的格式化引擎。
不隶属于 StackOverflow