2

It is my understanding that iOS uses ICU under the hood. I'd like to access the functionality of ICU's MessageFormat for strings involving numbers and plurals, for example,

There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.

Does iOS expose this capability? If so how do I use it? I thought I might be able to write

[NSString stringWithFormat: "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.", n];

or

[NSString stringWithFormat: "There {0,plural, =0{are no files}=1{is one file}other{are %ld files}}.", n];

But these formats (ICU4J and ICU, respectively) do not work.

user371320
  • 1,388
  • 14
  • 23

2 Answers2

1

iOS, like OS X, does include /usr/lib/libicucore.dylib. However, it's not a supported public API on either OS. See this old message for an explanation of how to use it on OS X, and what are the potential issues.

On iOS, I guess there is the additional issue that Apple might reject your application for using unsupported APIs.

LaC
  • 12,624
  • 5
  • 39
  • 38
  • Nice. Seven years ago they were considering this. If I use an unsupported library my app map be rejected. If I bundle ICU in my app it might be rejected (http://stackoverflow.com/questions/2427838/iphone-app-rejection-for-using-icu-unicode-extensions) How are developers localizing plurals on iOS? – user371320 Apr 28 '12 at 23:13
  • @user371320 the question you linked was about linking to the unsupported included library, not about bundling it. You can build ICU and link it statically into your application. – LaC Apr 28 '12 at 23:25
  • Sure, but apparently I'd need to modify the ICU code: "If you have defined methods in your source code with the same names as the above mentioned APIs, we suggest altering your method names so that they no longer collide with Apple's private APIs to avoid your application being flagged with future submissions." – user371320 Apr 28 '12 at 23:29
0

The formatting rules for stringWithFormat do not include ICU formatting. You would need to call a different function for such things. AFAIK iOS does not expose ICU MessageFormat-compatible NSString APIs.

However, given that you did have access to the APIs (either via publicly exposed or bundling a custom build of ICU with your application), for such message formatting to work the ICU library must have access to resource files that:

  • Contain choice data for the locale(s) you're interested in
  • Are up to date enough to support the choice format you're attempting to use

See the ICU documentation for more information.

NuSkooler
  • 5,391
  • 1
  • 34
  • 58