Skip to content

Commit

Permalink
SelectFormat
Browse files Browse the repository at this point in the history
Fixes the notification offset message to use locale specific quantity and gender rules (api24+); e.g. "del", "de la", "de los", "de las".
#522
  • Loading branch information
forrestguice committed Jan 1, 2022
1 parent 266d609 commit 0a59dd5
Show file tree
Hide file tree
Showing 18 changed files with 720 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
Copyright (C) 2018-2020 Forrest Guice
Copyright (C) 2018-2021 Forrest Guice
This file is part of SuntimesWidget.
SuntimesWidget is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -31,6 +31,7 @@
import android.content.Context;
import android.content.Intent;

import android.icu.text.MessageFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
Expand Down Expand Up @@ -649,17 +650,10 @@ public static Notification createNotification(Context context, @NonNull AlarmClo
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

String eventString = (alarm.event != null ? alarm.event.getShortDisplayString() : null);
String eventDisplay = eventString;
String eventDisplay = (alarm.event != null ? alarm.event.getLongDisplayString() : null);
if (alarm.offset != 0) {
if (eventString == null) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(alarm.timestamp);
eventString = utils.calendarTimeShortDisplayString(context, calendar).toString();
}
String offsetText = utils.timeDeltaLongDisplayString(0, alarm.offset).getValue();
int offsetStringResID = ((alarm.offset < 0) ? R.string.offset_before_msg : R.string.offset_after_msg);
eventDisplay = context.getString(offsetStringResID, offsetText, eventString);
eventDisplay = (alarm.event != null) ? formatOffsetMessage(context, alarm.offset, alarm.event)
: formatOffsetMessage(context, alarm.offset, alarm.timestamp);
}

String emptyLabel = ((eventDisplay != null) ? eventDisplay : context.getString(R.string.alarmOption_solarevent_none));
Expand Down Expand Up @@ -766,6 +760,45 @@ public static Notification createNotification(Context context, @NonNull AlarmClo
return builder.build();
}

public static String formatOffsetMessage(Context context, long offset, @NonNull SolarEvents event)
{
int i = event.ordinal();
String[] eventStrings = context.getResources().getStringArray(R.array.solarevents_long1);
String eventText = (i >= 0 && i <eventStrings.length) ? eventStrings[i] : event.name();
String offsetText = utils.timeDeltaLongDisplayString(0, offset).getValue();

if (Build.VERSION.SDK_INT >= 24) { // uses SelectFormat so translations match quantity and gender
int[] eventPlurals = context.getResources().getIntArray(R.array.solarevents_quantity);
String[] eventGenders = context.getResources().getStringArray(R.array.solarevents_gender);
int plural = (i >= 0 && i <eventPlurals.length) ? eventPlurals[i] : 1;
String gender = (i >= 0 && i <eventGenders.length) ? eventGenders[i] : "other";
return formatOffsetMessage(context, offset, offsetText, eventText, plural, gender);
} else return formatOffsetMessage(context, offset, offsetText, eventText);
}
public static String formatOffsetMessage(Context context, long offset, long timestamp)
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
String eventText = utils.calendarTimeShortDisplayString(context, calendar).toString();
String offsetText = utils.timeDeltaLongDisplayString(0, offset).getValue();

if (Build.VERSION.SDK_INT >= 24) { // uses SelectFormat so translations match quantity and gender
int plural = SuntimesUtils.is24() ? calendar.get(Calendar.HOUR_OF_DAY) : calendar.get(Calendar.HOUR);
String gender = context.getString(R.string.time_gender);
return formatOffsetMessage(context, offset, offsetText, eventText, plural, gender);
} else return formatOffsetMessage(context, offset, offsetText, eventText);
}
public static String formatOffsetMessage(Context context, long offset, String offsetText, String eventText) {
return context.getString(((offset < 0) ? R.string.offset_before_msg : R.string.offset_after_msg), offsetText, eventText);
}
@TargetApi(24)
public static String formatOffsetMessage(Context context, long offset, String offsetText, String eventText, int plural, String gender )
{
String pattern = context.getString(((offset < 0) ? R.string.offset_before_msg1 : R.string.offset_after_msg1));
//Log.d("DEBUG", "formatOffsetMessage: " + plural + " : " + gender + "\n" + pattern);
return new MessageFormat(pattern).format( new Object[] {offsetText, plural, gender, eventText} );
}

/**
* showNotification
* Use this method to display the notification without a foreground service.
Expand Down
36 changes: 35 additions & 1 deletion app/src/main/res/values-ca/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@
<item quantity="other">%1$s desprès</item>
</plurals>

<string name="offset_before_msg">%1$s abans %2$s</string> <!-- notification msg; '1h 30m before Sunset'; second arg is always non-plural --> <!-- TODO: review -->
<string name="offset_after_msg">%1$s desprès %2$s</string> <!-- notification msg; '1h 30m after end of evening civil twilight'; second arg is always non-plural --> <!-- TODO: review -->
<string name="offset_before_msg1">{0} abans {1, plural, one {{2, select, female {} other {}}} other {{2, select, female {} other {}}}}{3}</string> <!-- uses SelectFormat (api24+) --> <!-- TODO: review -->
<string name="offset_after_msg1">{0} desprès {1, plural, one {{2, select, female {} other {}}} other {{2, select, female {} other {}}}}{3}</string> <!-- uses SelectFormat (api24+) --> <!-- TODO: review -->

<string name="offset_button_before">abans</string> <!-- button label; [1h] [5m] [before] (-) -->
<string name="offset_button_after">desprès</string> <!-- button label; [1h] [5m] [after] (+) -->

Expand Down Expand Up @@ -1111,7 +1116,7 @@
<item>Migdia lunar</item>
<item>Mitjanit lunar</item>
</string-array>
<string-array name="solarevents_long"> <!-- one-to-one with solarevents_short -->
<string-array name="solarevents_long"> <!-- one-to-one with solarevents_short --> <!-- labels; ending may be ellipsed (so the beginning should be distinct) -->
<item>sortida de sol astronòmica</item>
<item>sortida de sol nàutica</item>
<item>hora blava de l\'alba (inici)</item>
Expand Down Expand Up @@ -1140,6 +1145,35 @@
<item>Migdia lunar</item>
<item>Mitjanit lunar</item>
</string-array>
<string-array name="solarevents_long1"> <!-- one-to-one with solarevents_long --> <!-- natural sounding phrases (nouns); used with solarevents_gender/quantity and SelectFormat patterns (e.g. offset_before_msg1) -->
<item>sortida de sol astronòmica</item>
<item>sortida de sol nàutica</item>
<item>hora blava de l\'alba (inici)</item>
<item>sortida de sol civil</item>
<item>hora blava de l\'alba (fi)</item>
<item>sortida de sol</item>
<item>hora daurada del fi de l\'alba</item>
<item>migdia</item>
<item>hora daurada de l\'inici del capvespre</item>
<item>posta de sol</item>
<item>hora blava nocturna (inici)</item>
<item>posta de sol civil</item>
<item>hora blava capvespre (fi)</item>
<item>posta de sol nàutica</item>
<item>posta de sol astronòmica</item>
<item>sortida lunar</item>
<item>posta lunar</item>
<item>lluna nova</item>
<item>quart creixent</item>
<item>lluna plena</item>
<item>quart minvant</item>
<item>equinocci de primavera</item>
<item>solstici d\'estiu</item>
<item>equinocci de tardor</item>
<item>solstici d\'hivern</item>
<item>Migdia lunar</item>
<item>Mitjanit lunar</item>
</string-array> <!-- TODO -->

<!-- Dialog: Enable GPS? -->
<string name="gps_dialog_msg">El GPS està desactivat. Activar-lo?</string>
Expand Down
36 changes: 35 additions & 1 deletion app/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@
<item quantity="few">%1$s po</item>
</plurals>

<string name="offset_before_msg">%1$s před %2$s</string> <!-- notification msg; '1h 30m before Sunset'; second arg is always non-plural --> <!-- TODO: review -->
<string name="offset_after_msg">%1$s po %2$s</string> <!-- notification msg; '1h 30m after end of evening civil twilight'; second arg is always non-plural --> <!-- TODO: review -->
<string name="offset_before_msg1">{0} před {1, plural, one {{2, select, female {} other {}}} other {{2, select, female {} other {}}}}{3}</string> <!-- uses SelectFormat (api24+) --> <!-- TODO: review -->
<string name="offset_after_msg1">{0} po {1, plural, one {{2, select, female {} other {}}} other {{2, select, female {} other {}}}}{3}</string> <!-- uses SelectFormat (api24+) --> <!-- TODO: review -->

<string name="offset_button_before">před</string> <!-- button label; [1h] [5m] [before] (-) -->
<string name="offset_button_after">po</string> <!-- button label; [1h] [5m] [after] (+) -->

Expand Down Expand Up @@ -1289,7 +1294,7 @@
<item>lunární poledne</item>
<item>lunární půlnoc</item>
</string-array>
<string-array name="solarevents_long"> <!-- one-to-one with solarevents_short -->
<string-array name="solarevents_long"> <!-- one-to-one with solarevents_short --> <!-- labels; ending may be ellipsed (so the beginning should be distinct) -->
<item>ranní astronomický soumrak (začátek)</item>
<item>ranní nautický soumrak (začátek)</item>
<item>ranní modrá hodina (začátek)</item>
Expand Down Expand Up @@ -1318,6 +1323,35 @@
<item>lunární poledne</item>
<item>lunární půlnoc</item>
</string-array>
<string-array name="solarevents_long1"> <!-- one-to-one with solarevents_long --> <!-- natural sounding phrases (nouns); used with solarevents_gender/quantity and SelectFormat patterns (e.g. offset_before_msg1) -->
<item>ranní astronomický soumrak (začátek)</item>
<item>ranní nautický soumrak (začátek)</item>
<item>ranní modrá hodina (začátek)</item>
<item>ranní občanský soumrak (začátek)</item>
<item>ranní modrá hodina (konec)</item>
<item>východ slunce</item>
<item>ranní zlatá hodina (konec)</item>
<item>pravé poledne</item>
<item>noční zlatá hodina (začátek)</item>
<item>západ slunce</item>
<item>noční modrá hodina (začátek)</item>
<item>noční občanský soumrak (konec)</item>
<item>noční modrá hodina (konec)</item>
<item>noční nautický soumrak (konec)</item>
<item>noční astronomický soumrak (konec)</item>
<item>východ měsíce</item>
<item>západ měsíce</item>
<item>Nov</item>
<item>první čtvrť</item>
<item>úplněk</item>
<item>třetí čtvrť</item>
<item>jarní rovnodennost</item>
<item>letní slunovrat</item>
<item>podzimní rovnodennost</item>
<item>zimní slunovrat</item>
<item>lunární poledne</item>
<item>lunární půlnoc</item>
</string-array> <!-- TODO: review -->

<!-- Dialog: Enable GPS? -->
<string name="gps_dialog_msg">GPS je vypnuta. Chcete ji zapnout?</string>
Expand Down
72 changes: 53 additions & 19 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@
<plurals name="offset_after_plural"> <!-- e.g. "30m after \n 5:30PM" .. the time component is shown on the next line -->
<item quantity="one">%1$s nach</item>
<item quantity="other">%1$s nach</item>
</plurals>
</plurals>

<string name="offset_before_msg">%1$s vor %2$s</string> <!-- notification msg; '1h 30m before Sunset'; second arg is always non-plural --> <!-- TODO: review -->
<string name="offset_after_msg">%1$s nach %2$s</string> <!-- notification msg; '1h 30m after end of evening civil twilight'; second arg is always non-plural --> <!-- TODO: review -->
<string name="offset_before_msg1">{0} vor {1, plural, one {{2, select, female {} other {}}} other {{2, select, female {} other {}}}}{3}</string> <!-- uses SelectFormat (api24+) --> <!-- TODO: review -->
<string name="offset_after_msg1">{0} nach {1, plural, one {{2, select, female {} other {}}} other {{2, select, female {} other {}}}}{3}</string> <!-- uses SelectFormat (api24+) --> <!-- TODO: review -->

<string name="offset_button_before">vor</string> <!-- button label; [1h] [5m] [before] (-) -->
<string name="offset_button_after">nach</string> <!-- button label; [1h] [5m] [after] (+) -->
Expand Down Expand Up @@ -1103,24 +1108,24 @@
<item>Mondmittag</item>
<item>Mondmitternacht</item>
</string-array>
<string-array name="solarevents_long"> <!-- one-to-one with solarevents_short -->
<item>Astronomische Morgendämmerung (Beginn)</item>
<item>Nautische Morgendämmerung (Beginn)</item>
<item>Morgendliche Blaue Stunde (Beginn)</item>
<item>Bürgerliche Morgendämmerung (Beginn)</item>
<item>Morgendliche Blaue Stunde (Ende)</item>
<item>Sonnenaufgang</item>
<item>Morgendliche Goldene Stunde (Ende)</item>
<item>Mittagshöhe</item>
<item>Abendliche Goldene Stunde (Beginn)</item>
<item>Sonnenuntergang</item>
<item>Abendliche Blaue Stunde (Beginn)</item>
<item>Bürgerliche Abenddämmerung (Ende)</item>
<item>Abendliche Blaue Stunde (Ende)</item>
<item>Nautische Abenddämmerung (Ende)</item>
<item>Astronomische Abenddämmerung (Ende)</item>
<item>Mondaufgang</item>
<item>Monduntergang</item>
<string-array name="solarevents_long"> <!-- one-to-one with solarevents_short --> <!-- labels; ending may be ellipsed (so the beginning should be distinct) -->
<item>Astronomische Morgendämmerung (Beginn)</item>
<item>Nautische Morgendämmerung (Beginn)</item>
<item>Morgendliche Blaue Stunde (Beginn)</item>
<item>Bürgerliche Morgendämmerung (Beginn)</item>
<item>Morgendliche Blaue Stunde (Ende)</item>
<item>Sonnenaufgang</item>
<item>Morgendliche Goldene Stunde (Ende)</item>
<item>Mittagshöhe</item>
<item>Abendliche Goldene Stunde (Beginn)</item>
<item>Sonnenuntergang</item>
<item>Abendliche Blaue Stunde (Beginn)</item>
<item>Bürgerliche Abenddämmerung (Ende)</item>
<item>Abendliche Blaue Stunde (Ende)</item>
<item>Nautische Abenddämmerung (Ende)</item>
<item>Astronomische Abenddämmerung (Ende)</item>
<item>Mondaufgang</item>
<item>Monduntergang</item>
<item>Neumond</item>
<item>Zunehmender Halbmond</item>
<item>Vollmond</item>
Expand All @@ -1132,6 +1137,35 @@
<item>Mondmittag</item>
<item>Mondmitternacht</item>
</string-array>
<string-array name="solarevents_long1"> <!-- one-to-one with solarevents_long --> <!-- natural sounding phrases (nouns); used with solarevents_gender/quantity and SelectFormat patterns (e.g. offset_before_msg1) -->
<item>Astronomische Morgendämmerung (Beginn)</item>
<item>Nautische Morgendämmerung (Beginn)</item>
<item>Morgendliche Blaue Stunde (Beginn)</item>
<item>Bürgerliche Morgendämmerung (Beginn)</item>
<item>Morgendliche Blaue Stunde (Ende)</item>
<item>Sonnenaufgang</item>
<item>Morgendliche Goldene Stunde (Ende)</item>
<item>Mittagshöhe</item>
<item>Abendliche Goldene Stunde (Beginn)</item>
<item>Sonnenuntergang</item>
<item>Abendliche Blaue Stunde (Beginn)</item>
<item>Bürgerliche Abenddämmerung (Ende)</item>
<item>Abendliche Blaue Stunde (Ende)</item>
<item>Nautische Abenddämmerung (Ende)</item>
<item>Astronomische Abenddämmerung (Ende)</item>
<item>Mondaufgang</item>
<item>Monduntergang</item>
<item>Neumond</item>
<item>Zunehmender Halbmond</item>
<item>Vollmond</item>
<item>Abnehmender Halbmond</item>
<item>Frühjahrs-Tag-Nacht-Gleiche</item>
<item>Sommersonnenwende</item>
<item>Herbst-Tag-Nacht-Gleiche</item>
<item>Wintersonnenwende</item>
<item>Mondmittag</item>
<item>Mondmitternacht</item>
</string-array> <!-- TODO: review -->

<!-- Dialog: Enable GPS? -->
<string name="gps_dialog_msg">GPS ist deaktiviert. Jetzt aktivieren?</string>
Expand Down
36 changes: 35 additions & 1 deletion app/src/main/res/values-eo/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
<item quantity="other">%1$s post</item>
</plurals>

<string name="offset_before_msg">%1$s antaŭ %2$s</string> <!-- notification msg; '1h 30m before Sunset'; second arg is always non-plural --> <!-- TODO: review -->
<string name="offset_after_msg">%1$s post %2$s</string> <!-- notification msg; '1h 30m after end of evening civil twilight'; second arg is always non-plural --> <!-- TODO: review -->
<string name="offset_before_msg1">{0} antaŭ {1, plural, one {{2, select, female {} other {}}} other {{2, select, female {} other {}}}}{3}</string> <!-- uses SelectFormat (api24+) --> <!-- TODO: review -->
<string name="offset_after_msg1">{0} post {1, plural, one {{2, select, female {} other {}}} other {{2, select, female {} other {}}}}{3}</string> <!-- uses SelectFormat (api24+) --> <!-- TODO: review -->

<string name="offset_button_before">antaŭ</string> <!-- button label; [1h] [5m] [before] (-) -->
<string name="offset_button_after">post</string> <!-- button label; [1h] [5m] [after] (+) -->

Expand Down Expand Up @@ -1018,7 +1023,7 @@
<item>plejsupro de Luno</item>
<item>plejmalsupro de Luno</item>
</string-array>
<string-array name="solarevents_long"> <!-- one-to-one with solarevents_short -->
<string-array name="solarevents_long"> <!-- one-to-one with solarevents_short --> <!-- labels; ending may be ellipsed (so the beginning should be distinct) -->
<item>astronomia sunleviĝo</item>
<item>naŭtika sunleviĝo</item>
<item>matena blua horo (komenco)</item>
Expand Down Expand Up @@ -1047,6 +1052,35 @@
<item>kulmino supra de Luno</item>
<item>kulmino malsupra de Luno</item>
</string-array>
<string-array name="solarevents_long1"> <!-- one-to-one with solarevents_long --> <!-- natural sounding phrases (nouns); used with solarevents_gender/quantity and SelectFormat patterns (e.g. offset_before_msg1) -->
<item>astronomia sunleviĝo</item>
<item>naŭtika sunleviĝo</item>
<item>matena blua horo (komenco)</item>
<item>civila sunleviĝo</item>
<item>matena blua horo (fino)</item>
<item>sunleviĝo</item>
<item>matena ora horo (fino)</item>
<item>tagmezo vera (kulmino)</item>
<item>vespera ora horo (komenco)</item>
<item>sunsubiro</item>
<item>vespera blua horo (komenco)</item>
<item>civila sunsubiro</item>
<item>vespera blua horo (fino)</item>
<item>naŭtika sunsubiro</item>
<item>astronomia sunsubiro</item>
<item>lunleviĝo</item>
<item>lunsubiro</item>
<item>nova Luno</item>
<item>kreskanta Lunduono</item>
<item>plena Luno</item>
<item>malkreskanta Lunduono</item>
<item>printempa tagnoktegaleco</item>
<item>somera sunekstremo</item>
<item>aŭtuna tagnoktegaleco</item>
<item>vintra sunekstremo</item>
<item>kulmino supra de Luno</item>
<item>kulmino malsupra de Luno</item>
</string-array> <!-- TODO -->

<!-- App Setting: locale mode -->
<string name="configLabel_locale">Agordoj de lingvo</string> <!-- group title -->
Expand Down
Loading

0 comments on commit 0a59dd5

Please sign in to comment.