iOS arabic localization issue - ios

I have a string which contains an arabic string. When device locale is in arabic, the string is displayed properly. But when device locale is in english, the string order is lost i.e it turns the string to rtl.
My localizable string is:
"%1$# has recharged your phone by %2$#."
When locale is in english the output is:
The output should have been:
برتيكشا has recharged your phone by 1500.
How do I do this?

Related

How to have superscript with notification title

I have some local notifications and I'd like local notification title to have some characters as superscript.
I'm not able to find anything on it so far.
Some help would be appreciated
Notification title supports only string not attributed string.
We usually show some kind of common symbol as super script such as TM - Trade Mark etc..
These kind of common symbols are available as unicode which you can directly use in a string
let content = UNMutableNotificationContent()
content.title = "Test \u{2122}"
In the above backward slash means some escape sequence and u means it's a unicode and you have to the unicode number into the flower bracket.
There are so many unicode symbols like this that can be used as a part of string and no need of attributed string at all.

Password validation with regex Arabic and English characters in swift

I want to validate the password to include at least 1 Arabic or English letter and at least 1 Arabic or English number and at leats 8 length password, my old code that was made for English only was like :
let passwordRegex = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$"
if (!NSPredicate(format:"SELF MATCHES %#",passwordRegex).evaluate(with: password)){
return false
}
and then i found this answer for Arabic characters and digits, then i tried to merge both like this :
let passwordRegex = "^(?=.*[A-Za-zء-ي])(?=.*٠-٩\\d)[A-Za-zء-ي٠-٩\\d]{8,}$"
if (!NSPredicate(format:"SELF MATCHES %#",passwordRegex).evaluate(with: password)){
return false
}
please advise what's wrong, thanks in advance
Since an English or Arabic letter regex (as described in this answer you linked to, also, see this answer, too) is [a-zA-Za-z\u0621-\u064A] and an English or Arabic digit regex is [0-9\u0660-\u0669] you may use
let passwordRegex = "^(?=.*[a-zA-Z\\u0621-\\u064A])(?=.*[0-9\\u0660-\\u0669])[a-zA-Za-z\\u0621-\\u064A0-9\\u0660-\\u0669]{8,}$"
NOTE: you do not need the outer ^ and $ anchors because MATCHES requires the pattern to match the whole string input.
Another way to match an Arabic letter with ICU regex used in Swift is to use [\p{L}&&[\p{script=Arabic}]] (it is an intersection inside a character class, it matches any letter but from the Arabic character set). Same with a digit: [\p{N}&&[\p{script=Arabic}]]. Then, the regex will look like
let passwordRegex = "^(?=.*[\\p{L}&&[\\p{script=Arabic}A-Za-z]])(?=.*[\\p{N}&&[\\p{script=Arabic}0-9]])[\\p{L}\\p{N}&&[\\p{script=Arabic}a-zA-Z0-9]]{8,}$"
So, here
[\\p{L}&&[\\p{script=Arabic}A-Za-z]] - any letter but it should belong to either ASCII letters or Arabic script
[\\p{N}&&[\\p{script=Arabic}0-9]] - any digit but either from 0-9 range or Arabic script
[\\p{L}\\p{N}&&[\\p{script=Arabic}a-zA-Z0-9]] - any letter or digit but only from the ASCII 0-9, A-Z, a-z and Arabic script.
Note also, that in order to match any letters, you may use\p{L} and to match any digits you may use \d (they are Unicode aware in ICU library). So, *in case t does not matter if the letters or digits are Arabic, English, Greek or whatever, you may use
let passwordRegex = "^(?=.*\\p{L})(?=.*\\d)[\\p{L}\\d]{8,}$"

Rails not displaying unicode character

If I print the string "hello\u1D175 \u1D176and goodbye", I expect a musical tie symbol to show. Instead I get "helloᴗ5 ᴗ6and goodbye".
I'm assuming I need to change how the string is read? Or the font?
Using the unicode decimal value for a musical tie symbol, the following should work
9835.chr(Encoding::UTF_8)
For example,
"hello #{9835.chr(Encoding::UTF_8)} and..."
Reference: https://unicodelookup.com/

Why Locale.current.identifier is invalid?

The problem arises when setup the "Language & Region" (Settings => General => Language & Region) as follows:
"iPhone Language" to "English (Canada)".
"Region" to "Jordan".
However, (as mentioned answer for: How to get detailed language of device in swift calling:
print(Locale.current.identifier)
Logs:
en_JO
which is an invalid local identifier (Logically speaking, Jordan is a Middle-East country which its native language is Arabic not English).
I also checked the availableIdentifiers:
print(Locale.availableIdentifiers)
and -obviously -it does not contains "en_JO".
Also, I tried:
if let regionCode = Locale.current.regionCode, let languageCode = Locale.current.languageCode {
print("\(languageCode)_\(regionCode)")
}
and the output was the same.
It seems that it has nothing to do with identifier validity, but how can I make sure to get a valid identifier? As an example, in my case the expected result should be:
en_CA
So what am I missing here?
There is nothing invalid about the identifier en_JO. The identifier is completely valid per the Unicode standard as an identifier for English in Jordan region.
However, that does not mean data for that region has to be available. The system is not required to have data for every crazy combination of language and region.
See Language Matching in the Unicode standard. If there is no data for the requested locale, a fallback locale is then used, in this case probably the locale en.
Every locale identifier includes a language code(e.g. en) and a region code(e.g. JO). This much is evident from Apple's documentation:
A locale ID identifies a specific region and its cultural
conventions—such as the formatting of dates, times, and numbers. To
specify a locale, use an underscore character to combine a language ID
with a region designator
This means your statement that en_JO is an invalid identifier is incorrect. It is formed because you have selected english as a language and region to Jordan.
Now, if you want to get only langauge part, you can get it by preferredLangauges,
let langId = Locale.preferredLanguages.first
or by collatorIdentifier on current Locale
let langId = Locale.current.collatorIdentifier // returns optional string

Where should my brackets be in relation to the text for Arabic languages?

Our application automatically modifies the layout of Arabic text when it is followed by a bracket and I was wondering whether this was the correct behaviour or not?
The application shows items in the following format:
[ID of structure](version)
So version 1.5 of the English structure "stackoverflow" would be displayed as:
stackoverflow(1.5)
Note: the brackets need to be displayed. There is no space between the ID and the first bracket. The brackets simply encompass the version. The brackets could have been any character but it's far too late to switch to a different character now!
This works fine for left to right languages, but for Arabic languages the structures appear in the form:
ستاكوفيرفلوو(1.0)
I am not an Arabic speaker and I need to know if this is actually correct. Is the Arabic format the equivalent of the English format or has something gone horribly wrong?
The text in Arabic should be shown like:
ستاكوفيرفلوو(1.0) ‏
I added the html entity of RLM / Right-to-left Mark ‏ in order to fix the text. You should do so if your application doesn't support Bidi native-ly. You can add the RLM by these ways:
HTML Entity (decimal) ‏
HTML Entity (hex) ‏
HTML Entity (named) ‏
How to type in Microsoft Windows Alt +200F
UTF-8 (hex) 0xE2 0x80 0x8F (e2808f)
UTF-8 (binary) 11100010:10000000:10001111
UTF-16 (hex) 0x200F (200f)
UTF-16 (decimal) 8,207
UTF-32 (hex) 0x0000200F (200f)
UTF-32 (decimal) 8,207
C/C++/Java source code "\u200F"
Python source code u"\u200F"
(note: StackOverflow right transliteration is ستاك-أوفرفلو)

Resources