Android String Compare fails with Turkish Locale
Monkey Forums/Monkey Bug Reports/Android String Compare fails with Turkish Locale
| ||
When doing something like this:If string1.ToUpper() = string2.ToUpper() Can fail with the Turkish language. Also the Get in a String Map can fail. I've created an extern for Android to get around the String compare: #If TARGET="android" Then Function EqualIgnoreCase:Bool(lhs:String, rhs:String) = "diddy.equalIgnoreCase" #Else Public Function EqualIgnoreCase:Bool(lhs:String, rhs:String) If lhs.ToUpper() = rhs.ToUpper() Return True End Return False End #End Java: static boolean equalIgnoreCase(String lhs, String rhs) { if (lhs.equalsIgnoreCase(rhs)) { return true; } return false; } But looks like I need to create my own Map class now as I dont want to alter the javatranslator.monkey file... |
| ||
We need to change the .ToUpper and .ToLower calls to use the locale. I've created custom methods and replaced all my calls: MX1: #If TARGET="android" Then Function DiddyToUpper:String(lhs:String) = "diddy.toUpper" Function DiddyToLower:String(lhs:String) = "diddy.toLower" #Else Public Function DiddyToUpper:String(lhs:String) Return lhs.ToUpper() End Function DiddyToLower:String(lhs:String) Return lhs.ToLower() End #End Android: static String toUpper(String lhs) { return lhs.toUpperCase(Locale.ENGLISH); } static String toLower(String lhs) { return lhs.toLowerCase(Locale.ENGLISH); } And I've created my own version of StringMap: Class DiddyStringMap<V> Extends Map<String,V> Method Compare:Int( lhs$,rhs$ ) Return CompareWithLocale(lhs, rhs) End End #If TARGET="android" Then Function CompareWithLocale:Int(lhs:String, rhs:String) = "diddy.compareWithLocale" #Else Public Function CompareWithLocale:Int(lhs:String, rhs:String) Return lhs.Compare(rhs) End #End Android: static int compareWithLocale(String lhs, String rhs) { String newLhs = lhs.toLowerCase(Locale.ENGLISH); String newRhs = rhs.toLowerCase(Locale.ENGLISH); return (newLhs.compareTo(newRhs)); } |
| ||
Thought about a better, simpler solution and it seems to work fine: Setting the locale for the activity:static void setLocale(String language, String country) { System.out.println("setLocale = " + language + " " + country); Activity activity = BBAndroidGame.AndroidGame().GetActivity(); Context context = activity.getApplicationContext(); Configuration config = context.getResources().getConfiguration(); Locale locale = new Locale(language, country); Locale.setDefault(locale); config.locale = locale; context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); } In Diddy's Oncreate and OnResume it now calls: SetLocale(localeLanguage, localeCountry) Field localeLanguage:String = "en" Field localeCountry:String = "US" |