0

When sending a push notification through GCM (switching to FCM soon) to an Android device, how can I localize numbers that appear in the message title/body based on the language of the phone?

Example Message:

John Doe 137

If the device language is set to Arabic the numbers should localize

Localized Message

John Doe ١٣٧

Since I don't know the users device language when sending the notification, how can I localize the number when the message is received? The onMessageReceived callback does not execute unless the application is running in the foreground, so I can not perform a string replacement on the numbers there.

For context Below is an example of the gcmNotification JSON I'm sending to GCM from my services which is taking advantage of some of the other localization params the payload allows you to use.

   "gcmNotification": {
     "title_loc_key": "some_title_key",
     "title_loc_args": "[\"john\", \"Doe\", 137]",
     "icon": "TheIcon",
     "body_loc_key": "some_key",
     "sound": "somesound",
     "color": "Blue",
     "collapse_key": "somekey",
    }
Stavros_S
  • 2,145
  • 7
  • 31
  • 75
  • Your code is receiving and formatting the message, correct? I assume that is the case as the message body contains "some_title_key", implying that you are handling the localization. – EJK Feb 13 '17 at 17:49
  • Thus your question boils down to - How would I format a number for Arabic, which has already been answered: http://stackoverflow.com/questions/31185993/android-display-digits-in-arabic-format – EJK Feb 13 '17 at 17:50
  • Possible duplicate of [Android: Display digits in Arabic format](http://stackoverflow.com/questions/31185993/android-display-digits-in-arabic-format) – EJK Feb 13 '17 at 17:50
  • Would the string replace and number format need to occur in the onMessageReceived event? – Stavros_S Feb 13 '17 at 17:52
  • That is up to you. It is certainly one place that you could do that. – EJK Feb 13 '17 at 22:48
  • @EJK So I'm finding that the onMessageReceived event is not called if the data is sent as a Notification type through GCM. Is there another method you recommend? – Stavros_S Feb 15 '17 at 20:16
  • If you are using a BroadcastReceiver, the message will be delivered to your app in the `onReceiveNotification` method. – EJK Feb 16 '17 at 20:13
  • note that not all Arabs use the Hindu-Arabic numerals, North African countries, except Egypt, use the Arabic Numerals (1234) – Aeiman Bakeer Aug 18 '18 at 12:30

2 Answers2

2

In general you don't try. Arabic numerals (the normal 0-9) are well understood. If you want to insist on trying, you can do it client side with a string replace. Or server side by posting your locale to it and letting the server translate before sending down.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I'm required to translate them I'm afraid. Would the string replace need to be handled in the onMessageReceived event on the client? – Stavros_S Feb 13 '17 at 17:48
1

To solve the issue, I ended up creating just a helper class with a method to handle the conversion. I run the message text through it and it returns the translated values. It has worked well for me so far.

public static class NumberHelper
{
    public static string ConvertToArabicNumbers(this string input)
    {
        return input.Replace('0', '\u0660')
            .Replace('1', '\u0661')
            .Replace('2', '\u0662')
            .Replace('3', '\u0663')
            .Replace('4', '\u0664')
            .Replace('5', '\u0665')
            .Replace('6', '\u0666')
            .Replace('7', '\u0667')
            .Replace('8', '\u0668')
            .Replace('9', '\u0669')
            .Replace('.', '\u066B');
    }
}
Stavros_S
  • 2,145
  • 7
  • 31
  • 75