LanguageIdentifierConverter { public static String toLanguageIdentifier(final Locale locale) { if (locale == null) { return null; } final StringBuilder str = new StringBuilder(); str.append(locale.getLanguage()); final String country = locale.getCountry(); if (!country.isEmpty()) { str.append('-'); str.append(country); } return str.toString(); } private  LanguageIdentifierConverter(); static String toLanguageIdentifier(final Locale locale); static Locale toLocale(final String identifier);  }
@Test public void testToLanguageIdentifier() { final String id1 = LanguageIdentifierConverter.toLanguageIdentifier(Locale.GERMAN); Assert.assertEquals("de", id1); final String id2 = LanguageIdentifierConverter.toLanguageIdentifier(Locale.US); Assert.assertEquals("en-US", id2); final String id3 = LanguageIdentifierConverter.toLanguageIdentifier(null); Assert.assertNull(id3); }