Browse Source

Fix crash for some default locales

master
Henning Hall 5 years ago
parent
commit
2aaabaf6bf
2 changed files with 15 additions and 7 deletions
  1. +12
    -4
      android/src/main/java/com/henninghall/date_picker/LocaleUtils.java
  2. +3
    -3
      android/src/main/java/com/henninghall/date_picker/props/LocaleProp.java

+ 12
- 4
android/src/main/java/com/henninghall/date_picker/LocaleUtils.java View File

@ -1,12 +1,8 @@
package com.henninghall.date_picker;
import android.os.Build;
import android.util.Log;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
public class LocaleUtils {
@ -38,6 +34,18 @@ public class LocaleUtils {
return ((SimpleDateFormat)format).toLocalizedPattern().replace(",", "");
}
public static Locale getLocale(String languageTag){
Locale locale;
try{
locale = org.apache.commons.lang3.LocaleUtils.toLocale(languageTag);
} catch (Exception e ){
// Some locales can only be interpreted from country string (for instance zh_Hans_CN )
String firstPartOfLanguageTag = languageTag.substring(0, languageTag.indexOf(""));
locale = org.apache.commons.lang3.LocaleUtils.toLocale(firstPartOfLanguageTag);
}
return locale;
}
}

+ 3
- 3
android/src/main/java/com/henninghall/date_picker/props/LocaleProp.java View File

@ -3,8 +3,8 @@ package com.henninghall.date_picker.props;
import android.os.Build;
import com.facebook.react.bridge.Dynamic;
import com.henninghall.date_picker.LocaleUtils;
import org.apache.commons.lang3.LocaleUtils;
import java.util.Locale;
@ -18,7 +18,7 @@ public class LocaleProp extends Prop {
}
static private Locale getDefaultLocale(){
return LocaleUtils.toLocale(getDefaultLanguageTag());
return LocaleUtils.getLocale(getDefaultLanguageTag());
}
static private String getDefaultLanguageTag(){
@ -34,7 +34,7 @@ public class LocaleProp extends Prop {
@Override
public Locale toValue(Dynamic value){
this.languageTag = value.asString().replace('-','_');
return LocaleUtils.toLocale(languageTag);
return LocaleUtils.getLocale(languageTag);
}
}

Loading…
Cancel
Save