2

I couldn't understand and find any sources that showed me the practical use of NSLocalizedString. Even Apple documentation..

NSLocalizedString(<#key: String#>, tableName: <#String?#>, 
bundle: <#NSBundle#>, value: <#String#>, comment: <#String#>)

Ok so lets say I created this in a file, how do I write this for different languages?
Do I create a localized version of the whole file like: en.thisfile.swift , es.thisfile.swift, fr.thisfile.swift?

Or do I create new languages in different bundles or tables?

NSLocalizedString("hello", tableName: "en", value: "hello", comment: "wtf is this anyways?")
NSLocalizedString("hello", tableName: "fr", value: "bonjour", comment: "wtf is this anyways?")
NSLocalizedString("hello", tableName: "es", value: "hola", comment: "wtf is this anyways?")

And then how do I use it in a random file after setting it up?

println("dat nslocalizedstring I created, which will automatically know what to write??")
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
  • 1
    You couldn't find any sources? There's a whole guide on this subject https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPInternational/Introduction/Introduction.html – Abizern Oct 30 '14 at 19:23
  • oh, should've added ios as a tag :/ – Esqarrouth Oct 30 '14 at 19:46
  • And it has the same documentation for iOS. https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/Introduction/Introduction.html A little searching goes a long way. – Abizern Oct 30 '14 at 22:39
  • which is still showing examples in objective-c. a little reading title and tags goes a long way before downvoting.. – Esqarrouth Oct 31 '14 at 07:47
  • 4
    Swift is new. Cocoa is an Objective-C framework. Your question is about a specific macro in Cocoa, hence objective-c. A little self study and less dependence on spoon feeding goes even further. – Abizern Oct 31 '14 at 07:50
  • Abizem, a little study of your own would reveal that the answer to this question is not contained anywhere in the link you provided. – Andrew Duncan Nov 10 '17 at 16:49

1 Answers1

11

The table is the name of the file that defines the string, for example, table "ErrorMessages" will define a file called ErrorMessages.strings.

If no table is specified, the file will be the default Localizable.strings. The format of the file is a list of the following:

/* This is displayed when the entity is not found. */
"MSG_object_not_found" = "Object not found. Please, try again.";

Where the first line is the comment, the value on the left side is the key that identifies the string and the value on the right side is the actual localized value.

Usually, you can use the English value as the key (the 2 parameter version of NSLocalizedString). The language name (en, fr, es) is not part of the method call.

All of this is there to make translation simple. Note that most translators won't understand or even see the application, that's why it's important to have a good comment to provide a context for the message. There are examples of funny translation all over the internet which were created because the context was missing and the translators didn't know what they were translating.

Basically, your code will contain the texts in your main language and then you will generate the .strings files from your code using genstring tool (See this for more info). Then you will give the files to your translator, add the translated files to your application (every language in a different folder) and the application will select the correct files at runtime depending on the language selected by the user.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Is it normal that this doesn't autocomplete MSG_object_not_found? I used #define stuff at ios7 and I always could access my stored strings, with autocomplete – Esqarrouth Oct 30 '14 at 11:26
  • @Esq It is absolutely normal cause it's just a string. You can't expect autocompletion in strings. Using #define is a possibility but using a constant `NSString` is much better. Using macros for string constants is bad code. – Sulthan Oct 30 '14 at 16:01
  • MSG_object_not_found didn't work when I write a non existing key, it prints out the key. – Esqarrouth Dec 08 '14 at 10:25
  • @Esq I din't mean it as a placeholder for strings. I meant it as an example of an error message that is localized. – Sulthan Dec 08 '14 at 11:03