[swift-users] Localization in Swift.

Zhao Xin owenzx at gmail.com
Tue Nov 1 21:50:46 CDT 2016


I think in Swift.

let count = 10
> let says = NSLocalizedString("
> ​blabla
> \(count)
> ​blabla
> ", comment: "
> ​blabla
> ")


​can be roughly interpreted as

let says = String.localizedStringWithFormat(NSLocalizedString("
> ​blabla
> %
> ​@
> ​blabla
> ", comment: "
> ​blabla
> "), count)


​So if Swift does not want to do much effort on this ​, it could just find
every localized string that is with  '\(foo)' in `NSLocalizedString` and
converted to `String.localizedStringWithFormat(NSLocalizedString...`
internally.

Zhaoxin


On Wed, Nov 2, 2016 at 12:08 AM, Jens Alfke <jens at mooseyard.com> wrote:

>
> > On Nov 1, 2016, at 1:53 AM, Zhao Xin via swift-users <
> swift-users at swift.org> wrote:
> >
> > I began to realize that` \(count)` was not dealed well in localization.
> The compiler calculated the full string then looking for the translation,
> instead of looking for the translation first.
>
> NSLocalizedString was designed (in the 1990s) to be used with methods like
> String(format:…) that take printf-style “%”-substituted format strings.
> Swift’s string interpolation is obviously a different mechanism entirely.
>
> I suspect that Swift interpolation won’t work well for localized strings
> because the string and the code are so tightly connected. Localization very
> often needs to change the order of parameters, for instance. It’s also
> unclear where things like number formatting happen in Swift interpolation;
> when localizing a string, the conversion needs to be done using the same
> locale as the string lookup, which might not happen if the string-to-number
> conversion is separate and uses the default locale.
>
> —Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161102/ee3dec3b/attachment.html>


More information about the swift-users mailing list