如何将Java字符串转换为不同的语言

问题描述:

目前,我正在将一些文本写为pdf.根据用户的语言环境,必须更改语言(例如:西班牙语,中文,德语).

Currently I am writing some text to pdf. based on the user locale, the language must be changed(ex: spanish, chineese, german).

此转换是否有任何API?更好的方法?

Is there any API for this conversion? and Better approach?

您将为每个需要翻译的字符串提供一个默认键:

You would have a default key for each String you need translated:

title=The *
content=The * is an interesting site.

这将存储在EN语言环境的text_EN.properties中.

This would be stored in text_EN.properties for the EN locale.

对于其他每种语言,您将拥有text_.properties.示例text_FR.properties:

For each other language, you would have text_.properties. Example text_FR.properties:

title=Le *
content=Le * est un site interessant.

(标题,内容和其他键应该是您的键,并且在所有属性文件中都应该相同,因为这是您访问它们的方式.)

(title, content, and other keys should are your keys and should be the same in all your properties files, since that is how you access them.)

当您要将文本写入pdf时,您将根据需要的语言环境/语言从相关的属性文件中获取每个值.

When you are about to write the text to the pdf, you would acquire each value from the relevant properties file, based on the locale/language you need.

如果您不想自己做,可以使用不同的模板语言.我想到了Thymeleaf,但我认为也有Velocity.

There are different templating languages you can use for this if you don't want to do it yourself. Thymeleaf comes to mind, but I think there is also Velocity.