Generating Translations Automatically
Liferay DXP supports 50 languages out-of-the-box. Each locale has its own language properties file containing keys for its language.
When you create an application with custom language keys, it’s tedious and difficult to translate them for all these different languages. Fortunately, you can use Liferay’s Language Builder tool to generate translations automatically. Language Builder generates translations using Microsoft’s Translator API.
The accuracy and correctness of machine translation can vary from language to language. Use them at your own risk.
See the Sample Project
Start a new Liferay DXP instance by running
docker run -it -m 8g -p 8080:8080 liferay/dxp:2024.q2.11
Sign in to Liferay at http://localhost:8080 using the email address test@liferay.com and the password test. When prompted, change the password to learn.
Then, follow these steps:
-
Download and unzip Automatically Generating Translations.
curl https://resources.learn.liferay.com/dxp/latest/en/liferay/development/liferay-internals/extending-liferay/customizing-localization/liferay-m7d6.zip -O
unzip liferay-m7d6.zip
-
Open the
gradle.properties
file in the module root folder. Add your Microsoft Translator subscription key:microsoft.translator.subscription.key=[your unique key]
Note for Language Builder to work, you must obtain your own Microsoft Translator subscription key.
-
From the module root, run Language Builder.
./gradlew buildLang
In your terminal window, Language Builder generates the different language properties files. Note, if you see the error
Translation is disabled because credentials are not specified
, double-check that your subscription key is correct. -
Navigate to the module’s
src/main/resources/content
folder to view the translated properties files.
Adding Your Language Keys
The sample project has one language key in the Language.properties
file. Add as many language keys as you need for your application, then run Language Builder.
To deploy the language keys to Liferay, see Overriding Global Language Translations with Language Properties. The instructions are the same whether overriding existing keys or adding new keys.
If you want to exclude some languages from being translated automatically, use the excludedLanguageIds
property in your project’s build.gradle
file. For example, to exclude French,
buildLang {
excludedLanguageIds = ["fa"]
}
To learn more about the Language Builder tool and additional configuration, see Lang Builder Gradle Plugin.