胸腺 - 填充ANG获取语言属性值

发布于 2025-01-29 04:47:42 字数 768 浏览 3 评论 0原文

情况

我正在使用带有百里叶的弹簧靴来填充我的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>

我想实现的目标?

  1. 我想使用胸腺到达正确的语言属性文件
  2. 填充带有定义变量的{name}变量。模板应基于htmls中的可变名称,例如:hashmap:&lt;“ name”,“ fooname”&gt;
  3. 将填充的文本恢复为字符串。我没有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?

  1. I want to use thymeleaf to reach the correct language property file
  2. Populate the {name} variable with a defined variable. The templating should be based on variable name like in HTMLs eg: hashmap: <"name", "FooName">
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凤舞天涯 2025-02-05 04:47:42

是的,你可以!

您需要在资源目录中的消息文件中配置messageource bean,例如:
spring.messages.basename =路径/到/语言,假设您的属性文件位于path/to/to/to/to/to/languaice(_en).properties
给定此bean,无论您需要在哪里翻译字符串,都可以注入MessageSource的实例,并使用它来获取给定消息的翻译

public class I18NHelper {

    private final MessageSource messageSource;

    public I18NHelper(final MessageSource messageSource) {
        this.messageSource = messageSource;
    }
    
    public String translate(String key, String name) {
        return messageSource.getMessage(key, new Object[] {name}, Locale.ENGLISH);
    }
}

字符串注入和打电话给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 at path/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 message key:

public class I18NHelper {

    private final MessageSource messageSource;

    public I18NHelper(final MessageSource messageSource) {
        this.messageSource = messageSource;
    }
    
    public String translate(String key, String name) {
        return messageSource.getMessage(key, new Object[] {name}, Locale.ENGLISH);
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文