Introduction
Ontimize Boot’s remote internationalization service (i18n) allows you to manage the translations of the different languages of the application without having to modify the programs translation files, by storing the translations in the database. The interface implemented by the service is com.ontimize.jee.common.services.i18n.II18nService
.
Prerequisites
There are 2 options to follow this tutorial, clone the repository with the initial state and follow the tutorial step by step, or download the final example and see which files are new and which have been updated.
Initial project
/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-i18n-initial
Final example
/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-i18n
Configuring internationalization service in Ontimize Boot
In this complete tutorial, we are going to create a backend for an application from scratch, including the database with different tables, configuring the server, creating the necessary DAO files and implementing the service.
DB configuration
If the configuration table does not exist in the DB, the database tables necessary for the operation of the service are created, one in which the names of the translation bundles shall be stored, and another in which the translations and the bundle to which they belong shall be stored:
SQL (HSQL) Statement
In the first table (the one named TI18N) we will add a bundle to link all resources of the TI18N_VALUE table.
SQL (HSQL) Statement
In the case of the second table (the one named TI18N_VALUE) it will contain a column with the name of each Locale accepted by the application.
SQL (HSQL) Statement
Add Permissions
The com.ontimize.jee.common.services.i18n.II18nService
service has several methods that we will have to add to the permissions to be able to execute the methods.
SQL (HSQL) Statement
Add all permissions to the user role demo
.
SQL (HSQL) Statement
Server configuration
To configure this service, a new configuration fragment shall be added to the application.yml file.
This configuration indicates the keys and values to be stored in the database. The database table is the one corresponding to the bean described in the ref-bundle-repository: OCDatabaseBundleDao attribute.
Creation of DAO files
It is necessary to create the DAOs that reflect the new DB tables we have created.
First we will create the *.xml files.
OCDatabaseBundleDao.xml
OCDatabaseBundleValueDao.xml
In the *.java files we indicate that it is a repository whose name will be OCDatabaseBundleDao and OCDatabaseBundleValueDao respectively, using the @Repository
annotation. With the @Lazy
annotation, we indicate that the loading is delayed until it is completely necessary, and the @ConfigurationFile
annotation allows us to configure this DAO using the XML file and an additional file where some features common to several DAOs can be stored, such as the schema they belong to.
OCDatabaseBundleDao.java
OCDatabaseBundleValueDao.java
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- ICandidateService.java
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- OCDatabaseBundleDao.java
- OCDatabaseBundleValueDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- OCDatabaseBundleDao.xml
- OCDatabaseBundleValueDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Add I18n Rest Controller
I18nRestController.java
With the tag @RequestMapping
we indicate the route through which the rest service will receive the requests. With the tag @RequestBody
we receive the data that were sent in the rest request. In this class we create a method that retrieves the translations of a bundle that we have specified in the language that we have indicated.
- ontimize-examples
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- ICandidateService.java
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- OCDatabaseBundleDao.java
- OCDatabaseBundleValueDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- OCDatabaseBundleDao.xml
- OCDatabaseBundleValueDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- I18nRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
- ontimize-examples
Testing the i18n services
To test the i18n service we will use REST requests. For this, the following query will be executed: localhost:33333/i18n/bundle
Element | Meaning |
---|---|
localhost:33333 | Indicates the host |
/i18n | Indicates the service to be queried |
/bundle | Indicates the request mapping class |
The request types can only be POST and the request body has to have the following structure:
Element | Meaning |
---|---|
bundle | Indicates the class name of the bundle specified in the database |
lang | Indicates the language of the bundle. It needs to be lowercase. |
country | Indicates the country of the language. It needs to be uppercase. |
Request type | Query | URL | Service method | Body request |
---|---|---|---|---|
POST | query | localhost:33333/i18n/bundle | getBundle | Example below |
Body request:
The authorization used for these requests is authorization of the type BASIC. In this case, the access must be done with a user and password example:
User: demo
Password: demouser
Uses of I18n Rest Controller
The i18n interface has a few methods for managing bundles or translations. Next in this example, we will test the bundle values CRUD(Create, Read, Update, Delete) methods.
To simplify the code being written, three dots (…) may appear in some parts of the code. This indicates that there may be previous code before and after those dots.
Query bundle values
To query the bundle values we create a method that receieves the bundle class, the language to query and the country of this language.
The code to query the values of a bundle is in the previous section.
Update bundle values
To update the bundle values we create a method that receieves the translations to modify of a bundle that we have specified.
Code
I18nRestController.java
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- ICandidateService.java
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- OCDatabaseBundleDao.java
- OCDatabaseBundleValueDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- OCDatabaseBundleDao.xml
- OCDatabaseBundleValueDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- I18nRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
REST request
The request to execute is the following: localhost:33333/i18n/bundle/update
Element | Meaning |
---|---|
localhost:33333 | Indicates the host |
/i18n | Indicates the service to be queried |
/bundle/update | Indicates the request mapping class |
The request types can only be PUT and the request body has to have the following structure:
Element | Meaning |
---|---|
key | Indicates the key of the value to be translated. |
bundleClass | Indicates the class name of the bundle specified in the database. |
bundleValues | Indicates the values of the translation to be modified. |
Body request:
Insert bundle values
The method to update the bundle values explained in the previous point also allows creating a new value in the case of the specified value does not exist in the database.
DatabaseI18nEngine.java
Delete bundle values
To delete the bundle values we create a method that receives the bundle class and the key of the bundle values.
I18nRestController.java
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- ICandidateService.java
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- OCDatabaseBundleDao.java
- OCDatabaseBundleValueDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- OCDatabaseBundleDao.xml
- OCDatabaseBundleValueDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- I18nRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
The request to execute is the following: localhost:33333/i18n/bundle/delete
Element | Meaning |
---|---|
localhost:33333 | Indicates the host |
/i18n | Indicates the service to be queried |
/bundle/delete | Indicates the request mapping class |
The request types can only be DELETE and the request body has to have the following structure:
Element | Meaning |
---|---|
bundleClass | Indicates the class name of the bundle specified in the database. |
key | Indicates the key of the value to be translated. |
Body Request