-XLIFF (XML Localization Interchange File Format) > https://en.wikipedia.org/wiki/XLIFF
XLIFF .xlf files replace the older format of .po files (there is an article on this site), adding some new features.
Let's configure our Maven Java project for using XLIFF files for language translation, the relevant project structure is:
src/main/resources/xlf/
en.xlf
es.xlf
pl.xlf
pom.xml
The following Maven dependency will allow our program to use the .xlf translation resources:
<dependency>
<groupId>com.lyncode</groupId>
<artifactId>xliff-translation</artifactId>
<version>1.0.2</version>
</dependency>
private static final String RESOURCE_FILE_XLF = "src/main/resources/xlf/";
private static final String FORMAT_FILE_XLF = ".xlf";
/**
*
* @param key
* Eg: "user_not_found"
* @param ianaLangCode
* Eg: "en"
* @param parameters
* Value 1 to include the translated text, value 2 to include the
* translated text... value n to include the translated text
* @return parameterized translated text
*/
public static String getTextByIanaCode(String key, String langIanaCode, Object... parameters) {
String value = null;
FileInputStream input = null;
XLIFF xlif = null;
try {
input = new FileInputStream(RESOURCE_FILE_XLF + langIanaCode + FORMAT_FILE_XLF);
} catch (FileNotFoundException e) {
LOGGER.error("file not found");
throw new CustomException("Msg", e);
}
try {
xlif = XLiffUtils.read(input);
} catch (XliffException e) {
LOGGER.error("not read file");
throw new CustomException("Msg", e);
}
value = xlif.getTarget(key);
try {
input.close();
} catch (IOException e) {
LOGGER.error("file not closed");
//DOC. No exception thrown
}
value = MessageFormat.format(value, parameters);
LOGGER.info("key: " + key + " in language " + langIanaCode + " : " + value);
return value;
}