7ball
7ball

Reputation: 2315

Swift localization with possessive

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.

Upvotes: 2

Views: 893

Answers (2)

Vadivel
Vadivel

Reputation: 21

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"

Upvotes: 2

Sagar Chauhan
Sagar Chauhan

Reputation: 5823

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.

Upvotes: 2

Related Questions