胸腺 - 填充ANG获取语言属性值
情况
我正在使用带有百里叶的弹簧靴来填充我的HTML模板文件,并将结果作为字符串恢复。为此,我使用了SpringTemplateEngine
。
代码看起来像
Context context = new Context();
context.setVariables(myProperties);
return templateEngine.process(htmlTemplateName, context);
问题 我想实现类似的东西,但是使用Lagenager.property文件
,我有两个语言属性文件:language.properties and Language_en.properties,看起来像
my.value =此字符串包含虚拟名称= {name} < /code>
我想实现的目标?
- 我想使用胸腺到达正确的语言属性文件
- 填充带有定义变量的
{name}
变量。模板应基于htmls中的可变名称,例如:hashmap:&lt;“ name”,“ fooname”&gt; - 将填充的文本恢复为字符串。我没有HTML文件,我只想使用胸腺的模板机制。
问题
是可能的,我该怎么做? 语言中正确的格式是什么。
Circumstances
I'm using Spring Boot with Thymeleaf for populating my HTML template files and get back the result as a String. For that I used SpringTemplateEngine
.
The code looks like this
Context context = new Context();
context.setVariables(myProperties);
return templateEngine.process(htmlTemplateName, context);
The problem
I want to achieve something similar, but with language.property files
I have two language property files: language.properties and language_en.properties which looks like this
my.value = This a string containing a dummy name = {name}
What I want to achieve?
- I want to use thymeleaf to reach the correct language property file
- Populate the
{name}
variable with a defined variable. The templating should be based on variable name like in HTMLs eg: hashmap: <"name", "FooName"> - Get back the populated text as String. I don't have HTML file, I just want to use the templating mechanism of the thymeleaf.
Question
Is it possible and how can I do that?
What is the right formatting in language.properties if it's possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,你可以!
您需要在资源目录中的消息文件中配置
messageource
bean,例如:spring.messages.basename =路径/到/语言
,假设您的属性文件位于path/to/to/to/to/to/languaice(_en).properties
给定此bean,无论您需要在哪里翻译字符串,都可以注入
MessageSource
的实例,并使用它来获取给定消息的翻译字符串注入和打电话给GetMessage。
另外,有多种方法可以获取当前会话或系统的语言环境。我以英语语言环境为例。适应您的需求。
Yes, you can!
You need to configure a
MessageSource
bean with the correct basename to your message files in your resource directory, like:spring.messages.basename=path/to/language
, assuming your properties files are located atpath/to/language(_en).properties
Given this bean, wherever you need a translated string, you inject an instance of
MessageSource
and use that to get your translated string for a given messagekey
:Edit: Fixed the class to inject and the call to getMessage.
Also, there multiple ways to get the Locale of the current Session or the System. I used the English locale as an example. Adjust to your needs.