2

I'm dabbling with localization in Swift. It's been pretty straightforward until I have to deal with possessive.

Say I have these phrases in English:

Carol's car is red. Kris' car is red.

In French, they would be

La voiture de Carol est rouge La voiture de Kris est rouge

How would I set up my Localizable.strings (French) file? I'd imagine it's something like:

"key" = "La voiture de %@ est rouge";

But this doesn't really work.

7ball
  • 2,183
  • 4
  • 26
  • 61
  • What exactly does "not work"? Do you use NSLocalizedString and String.localizedStringWithFormat? – I assume that the real problem is not french but english, where you have to append `'` or `'s`. – Martin R Feb 21 '18 at 07:45
  • Sorry, let's forget about the English portion. What I'm looking for is a way to define some dynamic content my `strings` file. I'd like to to put `Carol` and `Kris` into my localized strings. – 7ball Feb 21 '18 at 07:54
  • See for example https://stackoverflow.com/q/35316655/1187415. – Martin R Feb 21 '18 at 07:56
  • Did you ever find a good solution to your problem? – Georg Mar 05 '18 at 17:21

2 Answers2

2

Yes, you can replace text with you required value. I have just using your flow of questions, so answer is same it is.

First create your localized string as follow:

"key" = "La voiture de #name# est rouge";

Then, when you required your string. i.e

let strFrench = Localizable.strings(French)

Localizable.strings(French), this is you are assuming, so I have wrote this.

Now replace name with your dynamic value as follow:

let str = strFrench.replacingOccurrences(of: “#name#”, with: “YourString”)

I hope this will work for you. Sorry for my bad English.

Sagar Chauhan
  • 5,715
  • 2
  • 22
  • 56
2

Try this method.. First you declare language bundle globally like as below

var languageBundle:Bundle? 

then you set path for your language ,

if let path  = Bundle.main.path(forResource: "Fr", ofType: "lproj") {
languageBundle =  Bundle(path: path)
} else {
languageBundle = Bundle(path: Bundle.main.path(forResource: "Base", ofType: "lproj")!)
}

then you assign the key name to your label

labelTerms.text = ((languageBundle?.localizedString(forKey: "labelAcceptTerms", value: "", table: nil) as String!))

Thats all!!

Before you proceed please confirm you followed below steps for creating localized string file

Select project —> Select Use base Internationalization — > add language —> Goto storyboard and add select the checkbox (New languages added) — > add a string file localized.string —> add list of strings in that --> Use the key name .localizedString(forKey:"addyourkeyname"

Vadivel
  • 21
  • 4