Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Localization from resources (#1196)
Browse files Browse the repository at this point in the history
* Added localization from resources

* Fix some localization errors

* Docs for localization

* Fix review comments
  • Loading branch information
polyn0m authored and macdonst committed Sep 14, 2016
1 parent e711b09 commit 2de9fc2
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 8 deletions.
56 changes: 56 additions & 0 deletions docs/PAYLOAD.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [Background Events](#push-message-arrives-with-app-in-background)
- [Tap Events](#user-clicks-on-notification-in-notification-center)
- [Android Behaviour](#android-behaviour)
- [Localization](#localization)
- [Images](#images)
- [Sound](#sound)
- [Stacking](#stacking)
Expand Down Expand Up @@ -64,6 +65,61 @@ Some ways to handle this *double* event are:

# Android Behaviour

## Localization

Plugin supported localization from resources for: title, message and summaryText.

You may use simple link to locale constant.

```javascript
{
"registration_ids": ["my device id"],
"data": {
"title": {"locKey": "push_app_title"},
"message": "Simple non-localizable text for message!"
}
}
```

Or use localization with formatted constants.

```javascript
{
"registration_ids": ["my device id"],
"data": {
"title": {"locKey": "push_app_title"},
"message": {"locKey": "push_message_fox", "locData": ["fox", "dog"]}
}
}
```

Here is an example using node-gcm that sends the above JSON:

```javascript
var gcm = require('node-gcm');
// Replace these with your own values.
var apiKey = "replace with API key";
var deviceID = "my device id";
var service = new gcm.Sender(apiKey);
var message = new gcm.Message();
message.addData('title', {"locKey": "push_app_title"});
message.addData('message', 'Simple non-localizable text for message!');
// Constant with formatted params
// message.addData('message', {"locKey": "push_message_fox", "locData": ["fox", "dog"]});
service.send(message, { registrationTokens: [ deviceID ] }, function (err, response) {
if(err) console.error(err);
else console.log(response);
});
```

Localization must store in strings.xml

```xml
<string name="push_app_title">@string/app_name</string>
<string name="push_message_fox">The quick brown %1$s jumps over the lazy %2$s</string>
<string name="push_summary_text">%%n%% new message(s)</string>
```

## Images

By default the icon displayed in your push notification will be your apps icon. So when you initialize the plugin like this:
Expand Down
67 changes: 59 additions & 8 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ public void onMessageReceived(String from, Bundle extras) {
Log.d(LOG_TAG, "onMessage - from: " + from);

if (extras != null) {
Context applicationContext = getApplicationContext();

SharedPreferences prefs = getApplicationContext().getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
SharedPreferences prefs = applicationContext.getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
boolean forceShow = prefs.getBoolean(FORCE_SHOW, false);
boolean clearBadge = prefs.getBoolean(CLEAR_BADGE, false);

extras = normalizeExtras(extras);
extras = normalizeExtras(applicationContext, extras);

if (clearBadge) {
PushPlugin.setApplicationIconBadgeNumber(getApplicationContext(), 0);
Expand All @@ -87,26 +88,28 @@ else if (forceShow && PushPlugin.isInForeground()) {
extras.putBoolean(FOREGROUND, true);
extras.putBoolean(COLDSTART, false);

showNotificationIfPossible(getApplicationContext(), extras);
showNotificationIfPossible(applicationContext, extras);
}
// if we are not in the foreground always send notification if the data has at least a message or title
else {
Log.d(LOG_TAG, "background");
extras.putBoolean(FOREGROUND, false);
extras.putBoolean(COLDSTART, PushPlugin.isActive());

showNotificationIfPossible(getApplicationContext(), extras);
showNotificationIfPossible(applicationContext, extras);
}
}
}

/*
* Change a values key in the extras bundle
*/
private void replaceKey(String oldKey, String newKey, Bundle extras, Bundle newExtras) {
private void replaceKey(Context context, String oldKey, String newKey, Bundle extras, Bundle newExtras) {
Object value = extras.get(oldKey);
if ( value != null ) {
if (value instanceof String) {
value = localizeKey(context, newKey, (String) value);

newExtras.putString(newKey, (String) value);
} else if (value instanceof Boolean) {
newExtras.putBoolean(newKey, (Boolean) value);
Expand All @@ -118,6 +121,49 @@ private void replaceKey(String oldKey, String newKey, Bundle extras, Bundle newE
}
}

/*
* Normalize localization for key
*/
private String localizeKey(Context context, String key, String value) {
if (key.equals(TITLE) || key.equals(MESSAGE) || key.equals(SUMMARY_TEXT)) {
try {
JSONObject localeObject = new JSONObject(value);

String localeKey = localeObject.getString(LOC_KEY);

ArrayList<String> localeFormatData = new ArrayList<String>();
if (!localeObject.isNull(LOC_DATA)) {
String localeData = localeObject.getString(LOC_DATA);
JSONArray localeDataArray = new JSONArray(localeData);
for (int i = 0 ; i < localeDataArray.length(); i++) {
localeFormatData.add(localeDataArray.getString(i));
}
}

String packageName = context.getPackageName();
Resources resources = context.getResources();

int resourceId = resources.getIdentifier(localeKey, "string", packageName);

if (resourceId != 0) {
return resources.getString(resourceId, localeFormatData.toArray());
}
else {
Log.d(LOG_TAG, "can't find resource for locale key = " + localeKey);

return value;
}
}
catch(JSONException e) {
Log.d(LOG_TAG, "no locale found for key = " + key + ", error " + e.getMessage());

return value;
}
}

return value;
}

/*
* Replace alternate keys with our canonical value
*/
Expand All @@ -143,7 +189,7 @@ private String normalizeKey(String key) {
/*
* Parse bundle into normalized keys.
*/
private Bundle normalizeExtras(Bundle extras) {
private Bundle normalizeExtras(Context context, Bundle extras) {
Log.d(LOG_TAG, "normalize extras");
Iterator<String> it = extras.keySet().iterator();
Bundle newExtras = new Bundle();
Expand Down Expand Up @@ -172,6 +218,8 @@ private Bundle normalizeExtras(Bundle extras) {

String value = data.getString(jsonKey);
jsonKey = normalizeKey(jsonKey);
value = localizeKey(context, jsonKey, value);

newExtras.putString(jsonKey, value);
}
}
Expand All @@ -189,14 +237,17 @@ private Bundle normalizeExtras(Bundle extras) {
String newKey = normalizeKey(notifkey);
Log.d(LOG_TAG, "replace key " + notifkey + " with " + newKey);

newExtras.putString(newKey, value.getString(notifkey));
String valueData = value.getString(notifkey);
valueData = localizeKey(context, newKey, valueData);

newExtras.putString(newKey, valueData);
}
continue;
}

String newKey = normalizeKey(key);
Log.d(LOG_TAG, "replace key " + key + " with " + newKey);
replaceKey(key, newKey, extras, newExtras);
replaceKey(context, key, newKey, extras, newExtras);

} // while

Expand Down
2 changes: 2 additions & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ public interface PushConstants {
public static final String CLEAR_ALL_NOTIFICATIONS = "clearAllNotifications";
public static final String VISIBILITY = "visibility";
public static final String INLINE_REPLY = "inlineReply";
public static final String LOC_KEY = "locKey";
public static final String LOC_DATA = "locData";
}

0 comments on commit 2de9fc2

Please sign in to comment.