Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

iphone - Advanced Localization with Omission of Arguments in Xcode

I have this formatted string that I am having a translator work on.

ENGLISH

"Check out the %1$@ %2$@ in %3$@: %4$@" = "Check out the %1$@ %2$@ in %3$@: %4$@"

GERMAN TRANSLATION

"Check out the %1$@ %2$@ in %3$@: %4$@" = "H?r Dir mal %2$@ in %3$@ an: %4$@";

These are passed to a [NSString stringWithFormat:] call:

//////////////////////////////////////
// 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;

With the desired and actual outcome of:

ENGLISH

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"

GERMAN

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"



THE PROBLEM The problem is that I was under the assumption that by using numbered variable in the -[NSString stringWithFormat:], I could do things like this, where the %1$@ variable is completely omitted. If you notice, the German translation of the format string does not use the first argument (%1$@) at all but it ("sound") still appears in the output string.

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is not a bug. Numbered arguments are not part of the C standard, but part of IEEE Std 1003.1, which says the following (emphasis mine):

The format can contain either numbered argument conversion specifications (that is, "%n$" and "*m$"), or unnumbered argument conversion specifications (that is, % and * ), but not both. The only exception to this is that %% can be mixed with the "%n$" form. The results of mixing numbered and unnumbered argument specifications in a format string are undefined. When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the (N-1)th, are specified in the format string.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...