Localised string array join?

localization

(Kiel Gillard) #1
let skills = ["nunchuck", "bow hunting", "computer hacking"]
let notLocalizedJoined = skills.joined(separator: ", ")

// nunchuck, bow hunting, computer hacking

How do I create a localised join?


(Kiel Gillard) #2

To be sure, it is not sufficient to pass a localised variation of ", " as the separator.


(Saagar Jha) #3

Do you have an example of the output produced by such a localized join?


(Ben Rimmington) #4

This is available in ICU (C++) and CLDR, but maybe not in Foundation yet.


(Tomáš Znamenáček) #5

I see, so this is about localizing the final and and possibly leaving out the separator:

let skills = ["nunchuck", "bow hunting", "computer hacking"]
let localizedJoin = skills.hypotheticalLocalizedJoined(separator: ", ")
// en: nunchuck, bow hunting, and computer hacking
// fr: nunchuck, bow hunting et computer hacking

I don’t think this is currently offered by the iOS/macOS SDKs.


(Lance Parker) #6

NSLocale/Locale has a groupingSeparator property that you could use


(Ben Rimmington) #7

@lancep That’s a numeric grouping separator, used by the NumberFormatter class.

  1> import Foundation
  2> Locale(identifier: "en").groupingSeparator
$R0: String? = ","
  3> Locale(identifier: "fr").groupingSeparator 
$R1: String? = " "

(Kiel Gillard) #8

Suggestion filed for CoreFoundation and Swift Foundation on radar as 37880652.

I essentially asked for a CFListFormatter to wrap icu::ListFormatter.


(Ben Rimmington) #9

There’s also a C API which wraps the C++ API. But the ICU list formatters don’t appear to support the non-standard list types such as or-short and unit-narrow.

The “/usr/lib/libicucore.dylib” on Apple platforms is mostly private APIs, except for the headers listed in minimalapis.txt (the macOS SDK doesn’t have any ICU headers). Projects such as swift-corelibs-foundation and WebKit can use their own copy of the ICU headers, but an App Store app might be rejected.


(Liam Nichols) #10

I built a library that uses the CLDR data to get around this - https://github.com/liamnichols/ListItemFormatter

FormatterKit also has it's own version however its not built to the Unicode spec whereas this version is