[swift-evolution] Localization support for string interpolation

Daniel Höpfl daniel at hoepfl.de
Thu Apr 21 08:34:02 CDT 2016


Sorry, I don't see the problem.

< and > are considered normal characters, that's fine.

The trick of DHLocalizedString is that it does not do the string 
interpolation. This is already handled by Swift.

If you call DHLocalizedString("<\(quote)> by <\(person)>"), the compiler 
generates something like:

DHLocalizedString(DHLocalizedStringStore("<",
                                          quote.description(),
                                          "> by <",
                                          person.description(),
                                          ">"),
                   tableName = nil,
                   bundle = nil)

DHLocalizedString concats the strings (stored in the 
DHLocalizedStringStore), replacing every even position by "%@". The 
resulting string is used as format argument of NSLocalizedString, using 
the even positions of the array as arguments. DHLocalizedString does not 
care about the contents of any of the strings parts.

All of this works because Swift always starts interpolated strings with 
a string literal, "\(4)" becomes ["", "4", ""] (not sure if it adds the 
empty string at the end, too). The parts that come from interpolation 
are always in the even positions.

My proposal has two parts:

a) Make NSLocalizedString work like DHLocalizedString does.
b) Make `xxx` a synonym to NSLocalizedString("xxx") (and give me a way 
to set the default values for tableName/bundle in this case)

On 2016-04-21 12:23, Adrian Zubarev via swift-evolution wrote:
> I know that :D I mean < and > are considered as normal characters of
> the string "<\(quote)> by <\(person)>“ so thats why this won’t
> work that way.
> 
> --
> Adrian Zubarev
> Am 21. April 2016 bei 11:32:05, Daniel Höpfl via swift-evolution
> (swift-evolution at swift.org) schrieb:
> 
>> Maybe using "<\(quote)> by <\(person)>"
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution


More information about the swift-evolution mailing list