Rishi Chaurasia
Rishi Chaurasia

Reputation: 528

Case Insensitive Localization in iOS

While implementing localization I realize that localization is case sensitive, what means:

 - "Cookie Selection" = "Sélection de biscuit";

 - "COOKIE SELECTION" = "SÉLECTION DE BISCUIT";

above two are different entities. If you localize like NSLocalizedString("cookie selection", comment: ""), this will not get localized as the key value mapping given for "cookie selection" is either for capitalized string or for Uppercased string.

Query: If I can make it case insensitive, in the sense like I will add only one pair for localization as

"Cookie Selection" = "Sélection de biscuit";

and this should work for all cases like below

 1. NSLocalizedString("Cookie Selection", comment: "")
 2. NSLocalizedString("COOKIE SELECTION", comment: "")
 3. NSLocalizedString("cookie selection", comment: "")

Note: -> without any custom method, use only NSLocalization.

Happy Coding

Upvotes: 0

Views: 1491

Answers (3)

Fogmeister
Fogmeister

Reputation: 77631

You could always remove the possibility of getting it wrong by making a struct to encapsulate your localised logic etc...

enum Strings: String {
    case cookieSelection

    var localized: String {
        return NSLocalizedString(self.rawValue, comment: "Something here")
    }
}

With something like this you can now keep track of all your localised strings in one place and also remove any chance of them being used incorrectly.

For usage do something like...

label.text = Strings.cookieSelection.localized

If you want a default value for the string you can add that to the case...

enum Strings: String {
    case cookieSelection = "Cookie Selection"
}

Upvotes: 2

HAK
HAK

Reputation: 2071

The simplest and most raw solution would be to always capitalize the key like so:

NSLocalizedString("cookie selection".uppercased(), comment: "Comment")

More cleaner approach would be to create a category on String class and use that method instead:

        extension String {
        public static func LocalizedString(key: String, comment: String) -> String {
            return NSLocalizedString(key.uppercased(), comment: comment)
        }
    }

// Usage

    String.LocalizedString(key: "cookie selection", comment: "")

Another nice approach would be to make a file of constants and use them instead of using hard coded strings.

Upvotes: 0

Keshav Raj
Keshav Raj

Reputation: 476

Nope....Better use a constant file where you will store all the localization constants and its values so by mistake localization constant value does not change by mistake(suppose you type cookie Selection instead of Cookie Selection) while calling. You will use then values in localizable files as key.

LocalizationConstants.swift

let CookieSelectionKey = "Cookie Selection"

Localizable.strings(English)

"Cookie Selection" = "Selection Of cookie";

Now call it like

  NSLocalizedString(CookieSelectionKey, comment: "")

Since it returns a string you can use string instance methods such as uppercased, capitalized(etc.) to change case accordingly.

Upvotes: 2

Related Questions