nutritionfactsorg / daily-dozen-android

Keep track of the foods that Dr. Greger recommends in his NYT's best-selling book, How Not to Die with this Android app
https://play.google.com/store/apps/details?id=org.nutritionfacts.dailydozen&hl=en
Other
274 stars 95 forks source link

unified imperial, metric strings (removes need for %s separate.) #204

Closed marc-medley closed 2 years ago

marc-medley commented 2 years ago

See Issue https://github.com/nutritionfactsorg/daily-dozen-android/issues/201 for discussion. The normalized update XML file are linked. The needed java modified is mentioned here but not implemented.

In the string.xml files, the only content structure change should be the string-array food_info_serving_sizes_* elements. Other string.xml changes should only be formatting into a common order to support comparison via a common text compare tools.

Existing release food_info_serving_sizes_* content structure:

<string-array name="food_info_serving_sizes_other_fruits">
    <item>%s1 medium-sized fruit</item>
    <item>1%s cut-up fruit</item>
    <item>%s dried fruit</item>
</string-array>
<string-array name="food_info_serving_sizes_other_fruits_imperial">
    <item />
    <item> cup</item>
    <item>¼ cup</item>
</string-array>
<string-array name="food_info_serving_sizes_other_fruits_metric">
    <item />
    <item>20 g</item>
    <item>40 g</item>
</string-array>

Proposed food_info_serving_sizes_* content structure is shown below. Fewer elements. Easer to read. One-to-one correspondence to what the translator sees in a spreadsheet row.

<string-array name="food_info_serving_sizes_other_fruits_imperial">
    <item>1 medium-sized fruit</item>
    <item>1 cup cut-up fruit</item>
    <item>¼ cup dried fruit</item>
</string-array>
<string-array name="food_info_serving_sizes_other_fruits_metric">
    <item>1 medium-sized fruit</item>
    <item>120 g cut-up fruit</item>
    <item>40 g dried fruit</item>
</string-array>

The affected *.java would appear to be:

private static int getServingSizesResourceId(final Context context,
                                             final String idSuffix) {
    return context.getResources().getIdentifier(
            "food_info_serving_sizes_" + idSuffix.toLowerCase(),
            "array",
            context.getApplicationInfo().packageName);
}

private static int getServingSizesResourceId(final Context context,
                                             final String foodName,
                                             @Units.Interface final int unitType) {
    return getServingSizesResourceId(context,
            foodName + (unitType == Units.IMPERIAL ? "_imperial" : "_metric"));
}

private static void initServingSizesForFood(final Context context, final String foodIdName) {
    // ... SNIP ...  

    // Dynamically load the string-arrays for food.
    // The naming convention below must be followed:
    //      food_info_serving_sizes_<formattedFoodIdName>
    //      food_info_serving_sizes_<formattedFoodIdName>_imperial
    //      food_info_serving_sizes_<formattedFoodIdName>_metric
    String[] servingSizeTexts = res.getStringArray(getServingSizesResourceId(context, formattedFoodIdName));
    String[] imperialServingSizes = res.getStringArray(getServingSizesResourceId(context, formattedFoodIdName, Units.IMPERIAL));
    String[] metricServingSizes = res.getStringArray(getServingSizesResourceId(context, formattedFoodIdName, Units.METRIC));

    final List<String> imperial = new ArrayList<>();
    final List<String> metric = new ArrayList<>();

    for (int i = 0; i < servingSizeTexts.length; i++) {
        imperial.add(String.format(locale, servingSizeTexts[i], imperialServingSizes[i]));
        metric.add(String.format(locale, servingSizeTexts[i], metricServingSizes[i]));
    }

    servingSizesImperial.put(foodIdName, imperial);
    servingSizesMetric.put(foodIdName, metric);

    //  ... SNIP ...
}