Member preview

Remember to localize for String conversion

This is a very simple subject, but it bites me. So I thought would be good to share.

I have an enumeration as below

public enum EnumType {
NONE,
PRIMARY,
SECONDARY
}

It is used to determined some analytical text to be sent. To ensure we don’t need to map this to a set of String that refers to the enum , I use the enum and convert to the needed String.

myEnumType.toString().toLowerCase();

All seems good, as I get none, primary and secondary.

If one device is in Turkish

However, one day we realize we have in our analytic, we have some prımary instead of primary. They looks the same, but the ı is not i. We then realize it is Turkish way of toLowerCase for converting Dotless I. So when we perform matching, they are categorized as different String from an English converted String.

To fix this

It is quite simple to fix this issue. We just need to ensure we have the appropriate Locale set if we want a definite String conversion. For our case, we’ll fix it to Locale.US.

myEnumType.toString().toLowerCase(Locale.US);

Note, this doesn’t apply to all. If in the case, you need to set the toLowerCase that match the user set languages accordingly (e.g. user typed text), using Locale would not be appropriate then.