[swift-users] Localization in Swift.

Brent Royal-Gordon brent at architechies.com
Wed Nov 2 15:43:57 CDT 2016


(Resending something that was accidentally off-list.)

> On Nov 1, 2016, at 11:09 PM, Jens Alfke via swift-users <swift-users at swift.org> wrote:
> 
> I don’t think the ExpressibleByStringInterpolation protocol provides enough information to make this work. It hands the implementation a list of values to concatenate, some of which are strings, but as far as I can tell there’s no way to tell which of those strings are the pieces of the string literal and which of them are the results of expressions.

There's actually a simple trick. The even-indexed elements are literal strings; the odd-indexed ones are interpolated values. This is true even if you have two interpolations adjacent to each other—there will be an empty string between them. I've used this for a few different things, including a LocalizableString type in Swift 2 and a SQLStatement type in Swift 3.

	https://gist.github.com/brentdax/79fa038c0af0cafb52dd
	https://github.com/brentdax/swift-sql/blob/master/Sources/SQLStatement.swift

> If I’m wrong about this, show me a workable implementation of it. :)

See above. :^)

> Also, ExpressibleByStringInterpolation is marked as being deprecated and will be “replaced or redesigned in Swift 4.0.” Maybe to solve this limitation?

I believe that making it easier to treat the literal and interpolated segments differently is one of the goals.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-users mailing list