9

Is there a way to get localized strings into an iOS playground? I have a number of right-to-left language translations that include format codes that I had to edit with a hex editor (to ensure the codes were in the correct byte order), and I wish to manually examine the output of the formatted string to make sure it worked.

As an aside, are there any Mac OS text editors that allow you to display Arabic and Hebrew in left-to-right mode?

0

3 Answers 3

1

I was able to get this working by making an en.lproj in the Resources folder of a playground (note that my system is in English, you may have to put it in the lproj for your language to get this to work on your system).

One thing to note is that if you're using a plurals stringsDict, I always forget that you have to specify that the key is a localized string. You can't just do:

String.localizedStringWithFormat("next_step", beer)

You have to do:

String.localizedStringWithFormat(NSLocalizedString("next_step", comment: ""), beer)

Here's a gist of what I did: https://gist.github.com/designatednerd/fdfb916cc4d4ad3f33c25e917a95a2be

0

Swift 3: playground example for testing localizations

//: Playground - noun: a place where people can play

import UIKit

//current locale identifier
NSLocale.current.identifier //"en_US"

//available identifiers
NSLocale.availableLocaleIdentifiers //["eu", "hr_BA", "en_CM", "en_BI" ...]

// particular locales    
let unitedStatesLocale = NSLocale(localeIdentifier: "en_US") 
let chinaLocale = NSLocale(localeIdentifier: "zh_Hans") 
let germanyLocale = NSLocale(localeIdentifier: "de_DE") 
let indiaLocale = NSLocale(localeIdentifier: "en_IN") 
let arabicLocale = NSLocale(localeIdentifier: "ar") 
let hebrewLocale = NSLocale(localeIdentifier: "he") 

//called in English
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: unitedStatesLocale.localeIdentifier)! //"English (United States)"
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: chinaLocale.localeIdentifier)! //"Chinese (Simplified)"
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: germanyLocale.localeIdentifier)! //"German (Germany)"
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: indiaLocale.localeIdentifier)! //"English (India)"
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: arabicLocale.localeIdentifier)! //"Arabic"
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: hebrewLocale.localeIdentifier)! //"Hebrew"

//particular locale called in german
germanyLocale.displayName(forKey: NSLocale.Key.identifier, value: unitedStatesLocale.localeIdentifier)! //"Englisch (Vereinigte Staaten)"

//particular locale called in arabic
arabicLocale.displayName(forKey: NSLocale.Key.identifier, value: unitedStatesLocale.localeIdentifier)! //"الإنجليزية (الولايات المتحدة)"

//particular locale called in hebrew
hebrewLocale.displayName(forKey: NSLocale.Key.identifier, value: unitedStatesLocale.localeIdentifier)! //"אנגלית (ארצות הברית)"

//representing Numbers
let pi:NSNumber = 3.14159265358979
var numberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.decimal

//differences in formatting in various locales   
numberFormatter.locale = unitedStatesLocale as Locale!
numberFormatter.string(from: pi ) //"3.142"

numberFormatter.locale = chinaLocale as Locale!
numberFormatter.string(from: pi ) //"3.142"

numberFormatter.locale = germanyLocale as Locale!
numberFormatter.string(from: pi ) //"3,142"

numberFormatter.locale = indiaLocale as Locale!
numberFormatter.string(from: pi ) //"3.142"

numberFormatter.locale = arabicLocale as Locale!
numberFormatter.string(from: pi ) //"٣٫١٤٢"

numberFormatter.locale = hebrewLocale as Locale!
numberFormatter.string(from: pi ) //"3.142"
1
  • Thanks for the reply - I was actually looking for a way to use my application's string files, though, not to print a single string in multiple locales.
    – JustinHK
    Apr 27, 2017 at 16:30
0

For all folks, that will end up here digging for answers, it's pretty easy:

let locale = Locale(identifier: "ru_RU")
let format = NSLocalizedString("localizedStringKey", comment: "")
let argument = 100 // some argument for localised string
print(String(format: format, locale: locale, argument))

Works with plurals, just put your .stringsdict in Resources subfolder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.