Search code examples
javaandroidlocale

Can't post "İ" letter to web server in English device language


In my project, that should post turkey country name as "TÜRKİYE" with uppercase letter to the web server. But it doesn't work when I change device language to English. Because It post "TÜRKIYE". Uppercase I. I must fix it and it should post "İ". How can I do this?

       String from1 = textV_fromCity_ac_search.getText().toString();
            String to1 = textV_toCity_ac_search.getText().toString();
            if (isValidData() && isValidDate() && hasSessionId()) {
                Intent intent = new Intent(SearchActivity.this, ResultActivity.class);
                intent.putExtra("SessionId", User.getInstance().getSessionId());
                intent.putExtra("PassengerNumber", textV_passenger_number.getText().toString());
                String[] dataX = getResources().getStringArray(R.array.cities);
                for (String aDataX : dataX) {
                    if (aDataX.equals(textV_fromCity_ac_search.getText().toString() + " (Türkiye)")) {
                        from1 = aDataX;
                    } else if (aDataX.equals(textV_toCity_ac_search.getText().toString() + " (Türkiye)")) {
                        to1 = aDataX;
                    }
                }
                intent.putExtra(MyConstants.BUNDLE_FROM, from1);
                intent.putExtra(MyConstants.BUNDLE_TO, to1);
                intent.putExtra(MyConstants.BUNDLE_ISDOMESTIC, from1.toUpperCase().contains("TÜRKİYE") && to1.toUpperCase().contains("TÜRKİYE"));
                startActivity(intent);
            }

Solution

  • You can uppercase String using Locale. By default, toUpperCase method uses system's default Locale. But you can change this by locale parameter.

    String uppercased = "Türkiye".toUpperCase(new Locale("tr","TR"));
    

    Edit: Ok, I'm editing an answer in order to clear things.

    Also you may want to look at "The Turkey Test": What is The Turkey Test?

    In an i18n perspective, localization of Turkish significantly different than other locales.

    For example, for latters:

    Lowercase I -> ı 
    Uppercase i -> İ
    

    In Java, default locale is provided by the system. But in your case, you want to use Turkish locale explicity. If you want to uppercase the string "Türkiye" to "TÜRKİYE" regardless of the system language, you should provide Turkish locale explicity.

    In order to achieve that, instead of using String#toUpperCase() method you need to use String#toUpperCase(Locale) method.