How to Translate Java Properties Files with PTC

Learn how to prepare Java .properties files for localization and translate them with PTC (Private Translation Cloud).

Translating your Java Properties (.properties) file allows you to expand your global reach. For Java applications, these files hold the user-facing text you want to translate, such as:

Button Labels
Error Messages
Other Interface Text

Tooltips, confirmations, instructions

PTC (Private Translation Cloud) makes it easy to translate Java .properties files with AI-driven Better Than Human Translation technology. It integrates with your software repository to keep translations up to date with every code change.

This tutorial will show you how to:

  • Prepare your .properties files for translation
  • Translate your files using PTC

How Java Properties Files Work

Java .properties files keep translatable text separate from your application’s code, simplifying localization. Each entry includes:

  • Key: A unique identifier used in your application
  • value: The text users see

Example source language file ( English)

properties

welcome.message=Welcome to our application!
button.submit=Submit
error.notfound=The requested resource was not found.

During translation, the keys (e.g., welcome.message) remain the same, while values change to match the target language.

Example translation ( French)

properties

welcome.message=Bienvenue dans notre application !
button.submit=Envoyer
error.notfound=La ressource demandée est introuvable.

Preparing Java Properties Files for Translation

By setting up your .properties file properly, you can save time and avoid errors during the translation process.

Step 1: Identify All User-Facing Text in Your Code 

To localize your Java application, start by finding all the user-facing text in your code. Look for:

  • Java code: Strings passed to methods like setText() or println(), or concatenated strings.
  • XML layouts: Text attributes for UI elements like buttons and labels.

Step 2:  Create a New Properties File

Once you’ve identified the text, organize it into a .properties file:

  1. Create the file: Save the file in your project’s src/main/resources directory with a meaningful name like messages.properties.
  2. Use descriptive keys: Choose keys that explain the purpose of each string to make your file easy to maintain.
  3. Include placeholders: Use placeholders like {0} or %s for dynamic content, such as usernames, dates, or numbers. 

Example of an organized Java .properties file:

# Navigation
navigation.home=Home
navigation.about=About Us

# Buttons
button.submit=Submit
button.cancel=Cancel

# Dynamic Messages
greeting=Hello, {0}! You have {1} new messages.

# Errors
error.invalid_credentials=Invalid username or password
error.timeout=Session timed out. Please log in again.

Step 3: Replace Hardcoded Text in Your Code

After preparing your .properties file, update your code to reference the keys instead of hardcoding strings.

  1. Use the ResourceBundle class to fetch values dynamically based on keys in the .properties file.
ResourceBundle bundle = ResourceBundle.getBundle("messages", Locale.getDefault());
  1. Replace every hardcoded string in your application with references to the .properties file.
Before

Hardcoded text

java

button.setText("Submit");
System.out.println("Welcome to our application!");
After

Text replaced with keys from the .properties file

java

button.setText(bundle.getString("button.submit"));
System.out.println(bundle.getString("welcome.message"));
  1.  If your .properties file includes placeholders (e.g., {0}), use MessageFormat to insert dynamic content at runtime.
java
String formattedMessage = MessageFormat.format(
    bundle.getString("greeting"), 
    "User123", 
    5
);
System.out.println(formattedMessage); 
// Output: Hello, User123! You have 5 new messages.
  1. Run the application to test your code. Make sure that all text displays correctly and placeholders work as expected.

Translating the Java Properties File

At this stage, you have your source language .properties file ready, containing all user-facing text. Now, it’s time to translate the file into your target languages.

PTC connects directly to your software repository, making it easy to translate .properties files and keep translations updated as your project changes.

Here’s how to translate your files:

Integrate Your Repository

Connect your software repository to PTC, and it will scan your project for .properties files.

Translate Your Files

Choose the languages you need, and PTC translates your .properties files using AI-powered Better Than Human Translation technology. Context from your project lets PTC create accurate translations.

Sync Translations Back

Once translations are complete, PTC pushes the updated .properties files back to your repository as a merge request. You can review and approve the changes to integrate them into your project.

PTC: Your Solution for Java Localization

In this guide, you’ve learned how to:

  • Prepare .properties files for translation by organizing your text as key-value pairs
  • Use PTC’s AI-driven technology to translate your files accurately
  • Keep translations updated by integrating PTC with your software repository

With PTC, localizing your application is simple, fast, and effective.

Try it free for 30 days and see the difference it makes for your project!

Scroll to Top