1

I am facing a weird issue where there are only few strings appearing in english when the locale is set to hungary. This is happening on very few devices of various OS versions.

Here are few devices I've tried on:

One Plus 8 running Android 12 (works perfectly fine) Google Pixel 5 running Android 12 (works well) Galaxy S10 running Android 12 (date strings appear in english when locale is HU) One Plus 5t running Android 10 (date strings appear in english when locale is HU) another One Plus 5t running Android 10 (works well)

and emulators of various android versions shows all the strings the way it should. i.e. all strings appear in hungary.

Here's the code:

strings.xml:

<string-array name="months_name">
     <item>Jan</item>
     <item>Feb</item>
     <item>Mar</item>
     <item>Apr</item>
     <item>May</item>
     <item>Jun</item>
     <item>Jul</item>
     <item>Aug</item>
     <item>Sep</item>
     <item>Oct</item>
     <item>Nov</item>
     <item>Dec</item>
 </string-array>
 <string-array name="full_months_name">
     <item>January</item>
     <item>February</item>
     <item>March</item>
     <item>April</item>
     <item>May</item>
     <item>June</item>
     <item>July</item>
     <item>August</item>
     <item>September</item>
     <item>October</item>
     <item>November</item>
     <item>December</item>
 </string-array>

<string name="st">st</string>
 <string name="nd">nd</string>
 <string name="rd">rd</string>
 <string name="th">th</string>

strings-hu.xml:

 <string-array name="months_name">
        <item>Jan</item>
        <item>Febr</item>
        <item>Márc</item>
        <item>Ápr</item>
        <item>Máj</item>
        <item>Jún</item>
        <item>Júl</item>
        <item>Aug</item>
        <item>Szept</item>
        <item>Okt</item>
        <item>Nov</item>
        <item>Dec</item>
    </string-array>
    <string-array name="full_months_name">
        <item>január</item>
        <item>február</item>
        <item>március</item>
        <item>április</item>
        <item>Lehet</item>
        <item>június</item>
        <item>július</item>
        <item>augusztus</item>
        <item>szeptember</item>
        <item>október</item>
        <item>November</item>
        <item>December</item>
    </string-array>
    
    
    <string name="st"></string>
    <string name="nd"></string>
    <string name="rd"></string>
    <string name="th"></string>

DateFormatter.java:

package app.util;

import android.content.Context;

import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Locale;

public class DateFormatter {

    private static Integer date;
    private static Integer month;
    private static Integer year;
    private static String[] monthArray;
    private static String[] shortMonthArray;

    public static String formatDate(long milliseconds, Context context) {
        init(milliseconds, context);

        if (!BuildConfig.FLAVOR.equals(BuildConfig.Flavor_Hungarian)) {
            return getDateString(date, context) + " " + getMonth(month, true) + " " + numberFormatBasedOnLanguage(year);
        } else {
            return numberFormatBasedOnLanguage(year) + " " + getMonth(month, true) + " " + getDateString(date, context);
        }
    }

    public static String formatDateMonthAndYear(long milliseconds, Context context) {
        init(milliseconds, context);

        if (!BuildConfig.FLAVOR.equals(BuildConfig.Flavor_Hungarian)) {
            return getDateString(date, context) + "-" + getMonth(month, true) + "-" + numberFormatBasedOnLanguage(year);
        } else {
            return numberFormatBasedOnLanguage(year) + ". " + getMonth(month, true) + " " + getDateString(date, context);
        }
    }


    public static String formatDateAndMonth(long milliseconds, Context context,
            boolean suffixRequired) {
        init(milliseconds, context);
        if (suffixRequired) {
            if (!BuildConfig.FLAVOR.equals(BuildConfig.Flavor_Hungarian)) {
                return getDateString(date, context) + " " + getMonth(month, true);
            } else {
                return  getMonth(month, true) + " " + numberFormatBasedOnLanguage(date);
            }
        } else if (!BuildConfig.FLAVOR.equals(BuildConfig.Flavor_Hungarian)) {
            return numberFormatBasedOnLanguage(date) + " " + getMonth(month, true);
        } else {
            return getMonth(month, true) + " " + numberFormatBasedOnLanguage(date);
        }
    }

    public static String formatToDDMMMY(long milliseconds, Context context) {
        init(milliseconds, context);

        if (!BuildConfig.FLAVOR.equals(BuildConfig.Flavor_Hungarian)) {
            return numberFormatBasedOnLanguage(date) + " " + getMonth(month, false) + " " + numberFormatBasedOnLanguage(year);
        } else {
            return numberFormatBasedOnLanguage(year) + ". " + getMonth(month, true) + " " + getDateString(date, context);
        }
    }

    public static String formatToDDMMMYYYY(long milliseconds, Context context) {
        init(milliseconds, context);
        return numberFormatBasedOnLanguage(date) + "-"
                + (getMonth(month, false)).toUpperCase(Constants.LANGUAGE)
                + "-" + numberFormatBasedOnLanguage(year);
    }
    public static String numberFormatBasedOnLanguage(long value){
        if(BuildConfig.IS_NUMBER_FORMAT_ENABLED){
            NumberFormat nf= NumberFormat.getInstance(new Locale(Locale.getDefault().getLanguage()));
            nf.setGroupingUsed(false);
            return nf.format(value);
        }else{
            return ""+value;
        }
    }
    public static String numberFormatBasedOnLanguage(String value){
        if(BuildConfig.IS_NUMBER_FORMAT_ENABLED){
        NumberFormat nf= NumberFormat.getInstance(new Locale(Locale.getDefault().getLanguage()));
            nf.setGroupingUsed(false);
        return nf.format(Integer.parseInt(value));
        }else{
            return ""+value;
        }
    }
    public static String numberFormatGlobalBasedOn(String value){
        if(BuildConfig.IS_NUMBER_FORMAT_ENABLED){
            NumberFormat nf= NumberFormat.getInstance(new Locale(Constants.GLOBAL));
            nf.setGroupingUsed(false);
            return nf.format(Integer.parseInt(value));
        }else{
            return ""+value;
        }
    }
    private static void init(long milliseconds, Context context) {
        monthArray = context.getResources().getStringArray(
                R.array.full_months_name);
        shortMonthArray = context.getResources().getStringArray(
                R.array.months_name);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(milliseconds);
        date = cal.get(Calendar.DATE);
        month = cal.get(Calendar.MONTH);
        year = cal.get(Calendar.YEAR);
        Log.d("DateFormatter", "date:" + date + " month:" + month + " year"
                + year);
    }

    private static String getDateString(Integer date, Context context) {
        String extension = "";
        if (BuildConfig.FLAVOR.equals(BuildConfig.Flavor_Hungarian)) {
            return DateFormatter.numberFormatBasedOnLanguage(date) + extension;
        }
        if (date == 1 || date == 21 || date == 31) {
            extension = context.getString(R.string.st);
        } else if (date == 2 || date == 22) {
            extension = context.getString(R.string.nd);
        } else if (date == 3 || date == 23) {
            extension = context.getString(R.string.rd);
        } else if (date == 4 || (date > 4 && date < 21)
                || (date > 23 && date < 31)) {
            extension = context.getString(R.string.th);
        }
        return DateFormatter.numberFormatBasedOnLanguage(date) + extension;
    }

    private static String getMonth(Integer month, boolean fullMonth) {
        if (fullMonth) {
            return monthArray[month];
        }
        return shortMonthArray[month];
    }
}

Here we're setting the language for the whole application:

SplashScreenActivity.java:

    /**
     * This method is to set the locale language.
     */
    private void setLocale(String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }

The above code works perfect in most of the devices. But on few devices it fails to show the date in hungarian language. It's happening on various android OS versions, so I'm not able to pinpoint where exactly the problem is.

Also, I've tried setting the device language to hungarian language but still the date appears in english in spite of that.

Can anyone please help me with this problem?

Screenshot of res folder:

res folder

Thank you.

chutwik
  • 51
  • 2

0 Answers0