[swift-users] Localization in Swift.

Jens Alfke jens at mooseyard.com
Wed Nov 2 15:41:49 CDT 2016


> On Nov 2, 2016, at 12:50 PM, Dave Abrahams via swift-users <swift-users at swift.org> wrote:
> 
> In my opinion, we can and must do much better for Swift.  If there's
> something about “%” formatting that you particularly value, I'd like to
> know about it, so I can make sure it's accomodated.

It offers more control over formatting, like min/max widths, number base, decimal places, etc. Yes, you can do this in the code inside the interpolated string, but IMHO it’s awkward because it requires knowing a bunch of extra methods for string conversion, truncation, etc. It’s a lot easier for me to remember and type “%x” than it is to remember and type the method that converts an int to a hex string.

Also (and more importantly for localization) the formatting details are part of the localizable format string, not hardwired. One example of this is formatting currency, where a US localization would use “$%.2f” but other currencies might call for more or fewer decimal places. There are other examples where one might swap format strings for other purposes like different-width layouts for monospaced/terminal output.

There’s also a nonstandard extension used by Cocoa/CF’s formatters, that allows the parameters to be reordered. (I haven’t used it so I don’t know the syntax offhand.) This is of course important for localization, to follow a language’s grammar.

I think these features could be added to interpolation. Just as a quick idea, maybe a syntax that allows formatting metacharacters to be added at the start of the interpolation, like “Please pay $\((.2) total)” where the “(.2) specifies two decimal places, or “The address is \((x) addr)”.

—Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161102/89f70043/attachment.html>


More information about the swift-users mailing list