Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 742
Martin Schlagnitweit Avatar asked Aug 07 '26 21:08

Martin Schlagnitweit


2 Answers

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.

like image 110
nfechner Avatar answered Aug 10 '26 10:08

nfechner


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.
like image 28
Peter Lawrey Avatar answered Aug 10 '26 11:08

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!