Multi language

Updated at 1691037473000

Overview

In today's relatively flat world, the demand for creating websites that support multiple languages is very common. With EzyPlatform, you can easily program to support multiple languages for your website.

Implementation

You can provide multilingual support in two ways:

  1. Using it in HTML files.
  2. Using it in JavaScript.

Using in HTML files

You can follow these steps:

  1. Create language files messages_<language_name>.properties in the src/main/resources/messages directory. Note that the messages.properties file will be the default and in English.
  2. Use [[#{message_key}]] if using the in-tag method, for example <span>[[#{message_key}]]</span>, and we encourage you to use this approach. Alternatively, you can also use th:<attribute_name>="{message_key}", for example: <span th:text="#{message_key}" ></span>
  3. If your message key is a variable, you can use [[#{${variable_name}}]] inside the tag or #{${variable_name}} inside the attribute.
  4. If your message key has parameters, for example hello=Hello {0} {1}, you can use inside the tag:
[[${#messages.msg('message_key', value0, value1)}]]

Or inside the attribute:

${#messages.msg('message_key', value0, value1)}
  1. If you want to convert the message to lowercase, you can use inside the tag:
[[${#strings.toLowerCase(#messages.msg('message_key'))}]]

Or inside the attribute:

${#strings.toLowerCase(#messages.msg('message_key'))}

For HTML-formatted messages, you can use the th:utext attribute, for example:

 <p th:utext="#{home.welcome}"></p>

You can refer to the Thymeleaf documentation for more details.

Using in JavaScript

For admin, we will need to use it inside the <script> tag as follows:

<script th:fragment="common" th:inline="javascript">
/*<![CDATA[*/
ezyadmin.messages.message_key1 = /*[[#{message_key1}]]*/ '';
/*]]>*/
// other javascript code
</script>
ava

For web, we will need to use it also inside the <script> tag as follows:

<script th:fragment="common" th:inline="javascript">
/*<![CDATA[*/
ezyweb.messages.message_key1 = /*[[#{message_key1}]]*/ '';
/*]]>*/
// other javascript code
</script>

IDE does not recognize messages

There will be cases where the IDE, such as IntelliJ, does not recognize messages when we run the plugin from the IDE. The reason and solution are as follows:

Reason

This is because we are configuring it this way:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>${ezyplatform.home}/settings</directory>
        </resource>
        <resource>
            <directory>${ezyplatform.home}/web/resources</directory>
        </resource>
    </resources>
</build>

This leads to a situation where many messages.properties files are being overridden, causing the IDE not to know which file to choose and not to load the plugin's messages.

Solution

Ezyplatform has implemented a mechanism to load messages files through EZYPLATFORM_HOME, so you need to take the following steps.

  1. Export the plugin to EZYPLATFORM_HOME.
  2. Restart the admin and activate the plugin or theme you are developing.
  3. Return to build and restart the plugin in the IDE.

This method may take some time, so you can use an alternative solution.

Another solution

Inside the target directory, you will find the selected message files.

You can clean build your project again, and it is likely that the IDE will prioritize the message files inside your project.