Automatically Generating Language Files

If you already have a Language.properties file that holds language keys for your user interface messages, or even a language module that holds these keys, you’re in the right place. In this tutorial, you’ll explore the following capabilities:

  • Generating language properties files for each supported locale with a single command. This prevents you from having to create a language properties file for each locale manually. The same command also propagates the keys from the default language file to all translation files.

  • Generating automatic translations using Microsoft’s Translator Text API. This prevents you from translating each message manually.

Generating Language Files for Supported Locales

If you want to generate files automatically for all supported locales, you must make a small modification to your application’s build file.

  1. Make sure your module’s build includes the com.liferay.lang.builder plugin by putting it in your build script’s classpath. If you’re using Liferay Workspace, the Lang Builder is already available to your modules.

    Here’s what a configuration of the com.liferay.lang.builder plugin looks in a build.gradle file:

        buildscript {
            dependencies {
                classpath 'com.liferay:com.liferay.gradle.plugins.lang.builder:latest.release'
            }
    
            repositories {
                maven {
                    url "http://repository-cdn.liferay.com/nexus/content/groups/public"
                }
            }
        }
    
        apply plugin: "com.liferay.lang.builder"
    
        repositories {
            maven {
                url "http://repository-cdn.liferay.com/nexus/content/groups/public"
            }
        }
    
  2. Create (if necessary) a default Language.properties file in the src/main/resources/content folder.

  3. Run the gradlew buildLang task from your project’s root folder to generate default translation files.

    The generated files contain copies of all the keys and values in your default Language.properties files. Run the buildLang task each time you change the default language file.

    When the task completes, it prints BUILD SUCCESSFUL with this log output:

    Translation is disabled because credentials are not specified
    

    See the next section to learn how to provide credentials to enable translation services.

Now you can begin translating your application’s messages. If you want to configure your app to generate automatic translations using the Microsoft Translator Text API, keep reading.

Translating Language Keys Automatically

If you’ve configured the com.liferay.lang.builder plugin in your app, you’re almost there. Now you have to configure Microsoft’s Translator Text API so you can generate automatic translations of your language keys. You cannot, however, use Liferay’s Lang Builder to automatically translate language keys containing HTML (e.g., <em>, <b>, <code>, etc.). Language keys containing HTML are automatically copied to all supported language files.

  1. Generate a translation subscription key for the Microsoft Translator Text API. Follow the instructions here.

  2. Make sure the buildLang task knows to use your subscription key for translation by setting the translateSubscriptionKey property:

    buildLang {
        translateSubscriptionKey = "my-key"
    }
    

    For security reasons, you probably don’t want to pass them directly in your application’s build script. Instead, pass the credentials to a property that’s stored in your local build environment, and pass the property into your application’s build script.

     buildLang {
       translateSubscriptionKey = langTranslateSubscriptionKey
    }
    

So what would the complete buildLang configuration look like if you followed all the steps above?

buildscript {
    dependencies {
        classpath 'com.liferay:com.liferay.gradle.plugins.lang.builder:latest.release'
    }

    repositories {
        maven {
            url "http://repository-cdn.liferay.com/nexus/content/groups/public"
        }
    }
}

apply plugin: "com.liferay.lang.builder"

buildLang {
   translateSubscriptionKey = langTranslateSubscriptionKey
}

repositories {
    maven {
        url "http://repository-cdn.liferay.com/nexus/content/groups/public"
    }
}

Great! You can now generate language files and provide automatic translations of your language keys.

« Localizing Your ApplicationUsing Liferay's Language Settings »
この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています