Internationalization Wide

About this page

This page illustrates how to internationalize your application with RubyCocoa (0.4.2).

Localized nib files

  1. Create Localized nib file from Xcode
    • by pressing "Add localization" at the inspector window of the MainMenu.nib icon

Localized Strings

  1. Collect all strings that can be localized in your code
  2. Create Localizable.strings for the strings
  3. Create translated Localizable.strings for each localization
  4. Store Localizable.strings in *.lproj directories
  5. Edit Localizable.strings

Sample code to use localized strings

To obtain localized strings, you can call either OSX::NSLocalizedString(key) or OSX::Bundle.localizedStringForKey(key, :value, :tableName)

Say, there are two Localizable.strings both in English.lproj and Japanese.lproj as shown below.

Localizable.strings (English.lproj):

"SEARCHING" = "Searching packages...";
"INSTALLING" = "Installing %s";
"PACKAGES" = "Packages are found";
"DONE" = "Done";

Localizable.strings (Japanese.lproj):

"SEARCHING" = "パッケージを検索中...";
"INSTALLING" = "%s をインストール中";
"PACKAGES" = "個のパッケージが見つかりました";
"DONE" = "終了しました";

To obtain a localized string for "Searching packages...", use the following code.

localizedString = OSX::NSLocalizedString("SEARCHING").to_s

The code below also returns the same result.

bundle = OSX::NSBundle.mainBundle
localizedString = bundle.localizedStringForKey("SEARCHING", 
                              :value, "not found", 
                              :table, "Localizable").to_s)

How to get Locale (or Language) related information

To obtain the current locale identifier (e.g. en_US), use the code below:

locale = NSLocale.currentLocale.localeIdentifier.to_s

The following code obtains user's current localization (e.g. English):

bundle = OSX::NSBundle.mainBundle
localization = bundle.preferredLocalizations.to_a.shift.to_s

Tips

  • Use OSX::NSBundle.localizedStringForKey when you use *.strings other than Localizable.strings
    • Use OSX::NSLocalizedString otherwise (internally, these two are the same)
  • Don't forget to put ';' at the end of each line in Localized.strings files
    • Missing semicolon causes Warning when searching localized strings
    • Use "plutil *.strings" to check if there are errors
  • Avoid using NSBundle.bundleForClass(self) - may crash
  • class constants are sometimes implemented as methods
    (e.g. not OSX::NSLocaleLanguageCode but OSX.NSLocaleLanguageCode)

Leave a comment

Begin the comment with //pukiwiki if you want to write a comment in PukiWiki format.

You must be logged in to post a comment.