public class I18nUtils extends Object
i18n utility class.
Modifier and Type | Method and Description |
---|---|
static String |
extractLanguageCode(String languageString,
String defaultLanguageCode)
Extracts the two letter language code defined in ISO 639 from a specified string.
|
static com.google.common.collect.ImmutableMap<String,String> |
flattenMap(Map<String,Map<String,String>> i18nMap,
String propertyPrefix,
String propertySeparator)
Flattens an i18n map to a map with a key based on the keys of the nested maps and a value of the defined property.
|
static <T extends Localizable<T>> |
localize(Collection<T> localizableCollection,
org.springframework.context.MessageSource messageSource,
Locale locale)
Localizes a list of localizable classes and returns the localized versions of the specified instances.
|
public static com.google.common.collect.ImmutableMap<String,String> flattenMap(Map<String,Map<String,String>> i18nMap, String propertyPrefix, String propertySeparator)
Flattens an i18n map to a map with a key based on the keys of the nested maps and a value of the defined property. For example we have a nested i18n map as following: { "de": { "name":"Name", "description","Beschreibung" }, "en": { "name":"name", "description","description" } } The resulting map looks like this (given that the property prefix is 'i18n' and the property separator is '.'): { "i18n.de.name:"Name", "i18n.de.description:"Beschreibung", "i18n.en.name:"Name", "i18n.en.description:"description" }
i18nMap
- the i18n map to flattenpropertyPrefix
- the prefix which is added before each key in the new mappropertySeparator
- the separator which is added between the map keyspublic static <T extends Localizable<T>> com.google.common.collect.ImmutableList<T> localize(Collection<T> localizableCollection, org.springframework.context.MessageSource messageSource, Locale locale)
Localizes a list of localizable classes and returns the localized versions of the specified instances.
T
- the localizable class which implements the Localizable
interfacelocalizableCollection
- the instances which needs to be localizedmessageSource
- the message source which translates the i18n codes for the localizationlocale
- the specified locale to which the i18n codes are translatedpublic static String extractLanguageCode(String languageString, String defaultLanguageCode)
Extracts the two letter language code defined in ISO 639 from a specified string. The following formats are supported: <ul> <li>en-US,en;;q=0.5 -> HTTP header Accept Language</li> <li>en-US -> two letter language code with locale</li> <li>en -> two letter language code</li> </ul>
languageString
- the string to get the two letter language codedefaultLanguageCode
- the default code which is returned if the languageString
is nullCopyright © 2010 - 2019 edorasware ag. All Rights Reserved.