Search code examples
ios7string-formattingarabicpluralize

iOS 7 pluralization + Arabic language


The basic problem is that the following line:

[NSString stringWithFormat:@"باقي للشراء %d", 3];

returns a string with a form of "3 arabic_text".

When I write a line:

[NSString stringWithFormat:@"%d باقي للشراء "];

again returns a string with form of "3 arabic text".

When I write a create a string like this:

NSString *digitThree = @"3";
NSString *expressionString = [NSString stringWithFormat:@"باقي للشراء %@", digitThree];

expressionString's value becomes a string with a form of "3 arabic text".

But when I write a code like this:

NSString *someChar = @"d";
NSString *expressionString = [NSString stringWithFormat:@"باقي للشراء %@", someChar];

expressionString's value becomes a string with a form of "arabic text d".

This behaviour probably has something to do with the fact that digits are accepted in arabic alphabet, but Latin alphabet characters are not.

I am using iOS7 localized pluralization feature (Localized Property List File section [here][1]).

Localizable.stringsdict file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>%d days</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%1$#@num_days@</string>
        <key>num_days</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>

            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>

            <key>one</key>
            <string>يوم %d</string>

            <key>other</key>
            <string>أيام %d</string>
        </dict>
    </dict>




</dict>
</plist>

From the code I use this like:

[NSString localizedStringWithFormat:NSLocalizedString(@"%d days", @"'%d' denotes the number of days. The expression would, for example, evaluate to: '4 days'"), 1])

But I can never get this call to return "arabic text 1", it always returns "1 arabic text".

EDIT:

Thank you, Holax.

At first, your answer didn't seem like something useful. But now I see it is. I was confused by a couple of things.

1) Using NSLog() and printing NSString values during debugging. It seems that printing NSString values with Arabic content this way prints them in the opposite direction than the way they are displayed when put as a UILabel content.

2) The way the UILabel wraps content into multiple lines when the text involves arabic characters combined with digits. Specifically, in my case I had:

"1234567890128 redemption code", which was wrapped into:

"123456789012

arabic text 8"


Solution

  • in general, in the Localizable.string file you can play the actual translation like:

    e.g. in English:

    "%d day" = "Expiration: %d days";
    

    then in other Language:

    "%d day" = "<expiration in days>: %d"
    

    the key is the same, but the value can hold the formatter at different location.


    furthermore the Arabic language's direction is RTL, opposite to the English language on the screen which is LTR, therefore you may need to consider about that fact and count with it in your language file, like:

    <key>other</key>
    <string>%d أيام</string>