-2

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);
            }
Shayma
  • 59
  • 1
  • 8
  • 1
    A [mcve] should help here - we'd need to know what exactly you are doing. – Thomas Apr 01 '19 at 10:55
  • @Thomas Sorry. I thought this is an easy way for it. Whatever I will post my code. – Shayma Apr 01 '19 at 10:57
  • As a workaround, would the solution also work with `toLowerCase()` ? – JensV Apr 01 '19 at 11:10
  • @JensV No. That's not what I want. – Shayma Apr 01 '19 at 11:33
  • @Shayma if you think it's easy, then you should find it easy to find the answer! – Alastair McCormack Apr 01 '19 at 12:42
  • @AlastairMcCormack Look. You can't understand or I can't tell it. There is problem only the device don't post uppercase of i. It post uppercase of ü. The uppercase of ü is not english letter right? But it can post that ü. It can't post uppercase i. So its the specific problem. So, I thought there is an answer that I don't know. – Shayma Apr 01 '19 at 12:50
  • 1
    `ü` to `Ü` works because that's the default mapping just as `i` to `I` is. However, depending on the locale there _might_ be special mappings (such as in your case) and thus you need to provide the locale you want to be used or otherwise you just get the standard mapping. – Thomas Apr 01 '19 at 13:02
  • @Thomas Thanks for this explanation. – Shayma Apr 01 '19 at 13:05

1 Answers1

1

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.

Tolga Okur
  • 6,753
  • 2
  • 20
  • 19
  • The problem is when it post in device with English language. It don't post this "İ" and make it "I". But it post truth "Ü". I need post "İ" also. Thats the problem. – Shayma Apr 01 '19 at 11:53
  • `"Türkiye".toUpperCase(new Locale("tr","TR"));` outputs `TÜRKİYE`. When you uppercase with `locale` parameter, device language won't affect your result. Thing that I say, use `toUpperCase(Locale)` method instead of `toUpperCase()` method. – Tolga Okur Apr 01 '19 at 12:26
  • But this upperCase(Locale) method occurs syntax error. Also I get this TÜRKİYE from array.xml not from string. – Shayma Apr 01 '19 at 12:53
  • 2
    @Shayma your code contains `from1.toUpperCase().contains("TÜRKİYE")` - that indicates that `from1` contains lowercase characters. If you read the Javadoc on `toUppercase()` you'll see that the special handling of `i` in Turkish is explicitly mentioned - that's why you need to provide the locale for that special case because the system's default locale might produce another mapping, i.e. `i` -> `I`. – Thomas Apr 01 '19 at 13:00
  • @Thomas Ok. Yes I understand that system's default produce this mapping. Ok I will read Javadoc. – Shayma Apr 01 '19 at 13:03