简体   繁体   中英

Is there a dedicated Language Class in Java?

While refactoring one of my projects, i replaced all language ISO Codes like "en" and "de" Strings with the Locale Class and its constants Locale.ENGLISH and Locale.GERMAN to make it more refactor save and to minimize error sources. I use then locale.getLanguage() to get the ISO Code as String.

The problem that i have with this approach is the overhead of the Locale Class in form of the country and variant fields. I am considering writing my own Language Class to avoid this overhead.

Is it good practice to use a custom class, or is there already a dedicated Language Class that i missed?

You missed one of the basic priciples of programming: Don't reinvent the wheel.

Yes, the Locale class can do more than you need, but the overhead is usually extremely negligible. Also, using it enables other coders to instantly understand that part of your code.

The overhead is not that high. This gets every Locale.

long start = System.currentTimeMillis();
for (Locale l : Locale.getAvailableLocales())
    l.toString();
long time = System.currentTimeMillis() - start;
System.out.println(time + " ms.");

prints

15 ms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM