Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String resources Xcode + swift

I am new in iOS developing and in Swift language. And I try to make simple iOS application, and I need to have some string resources (for labels and text fields) in app. Of course, I can put this strings to my *.swift files as constants, but I think, it is a bad way. How can I do it? I need something as string resources in Android. I use Xcode 6 beta with swift.

TY for answers ;)

like image 886
mr_ivan777 Avatar asked Aug 11 '14 18:08

mr_ivan777


People also ask

What are resource strings?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.

How do I create a localizable string in XCode?

Recent versions of XCode doesn't create Localizable. strings file by default. To add Localizable. strings file, go to File->New->File , choose Strings File under Resource tab of iOS, name it Localizable.

How does NSLocalizedString work?

NSLocalizedString is a Foundation macro that returns a localized version of a string. It has two arguments: key , which uniquely identifies the string to be localized, and comment , a string that is used to provide sufficient context for accurate translation.


1 Answers

Your best bet is creating a file called Localizable.strings. In this file you can make localised string variables you can use throughout your application. Define strings like this;

"hello_world" = "Hello World!";

Use them in your Obj/C code like so;

NSLocalizedString(@"hello_world", nil);

Or in Swift;

NSLocalizedString("hello_world", comment: "")

p.s., I haven't tested this. Swift code might be faulty because of the fact I can't test this atm.

like image 197
bmeulmeester Avatar answered Sep 27 '22 16:09

bmeulmeester