Java Properties (.properties) files are simple text files that consist of key-value pairs. It is a versatile file format that can serve various purposes. In Java applications, one of the primary uses is to store localization messages. When used as a localization file, the key represents the message identifier, and the value represents the corresponding translation.
These files are mostly encoded using the UTF-8 and Latin-1 encoding standards. However, for better compatibility and a more convenient way to represent non-Latin characters, UTF-8 is recommended.
Note: The way localization messages are handled can vary between different localization libraries. The examples provided here are intended to be compatible with the ICU4J library.
! Java .properties file example
# The ICU Plural message that is used to display the number of items.
common.count = {0, plural, one {{0} item} other {{0} items}}
# The ICU Select message that is used to display the name of the device.
common.device = {0, select, mobile {Mobile} desktop {Desktop} other {Unknown}}
# The formatted date (e.g. 'Mar 3, 2023').
common.formatted-date = Date: {0, date, medium}
# The formatted number (e.g. '123,456').
common.formatted-number = Number: {0, number, integer}
# The formatted time (e.g. '7:33 PM').
common.formatted-time = Time: {0, time, short}
common.next = Next
common.previous = Previous
# Intro message split into two lines.
page.home.intro = Welcome to our website,\nwhere technology and innovation meet creativity and design.
# Welcome message that includes the user's first and last name.
page.home.subtitle = Welcome {0} {1}!
page.home.title = Home
# Support message written in multiple lines for enhanced readability
page.home.support = Welcome to our platform! \
We're thrilled to have you here. \
Please enjoy your stay, and if you need any assistance, don't hesitate to reach out.
Placeholders are used to dynamically insert values into translated messages. Placeholders are represented by a pair of curly braces ({}
) and are used in combination with a key that specifies the value to be inserted. At runtime, the application would retrieve the translated message using the message identifier and pass in the appropriate value for the placeholder.
# Example with a numbered placeholder
greeting = Hello, {0}!
# Example with a named placeholder
greeting = Hello, {name}!
Plural messages are crucial for accommodating the various ways that nouns and adjectives change based on quantity in different languages. They simplify the task of supporting different plural forms in localized messages for each language.
# Example with a numbered placeholder
counter = {0, plural, one {{0} item} other {{0} items}}
# Example with a named placeholder
counter = {count, plural, one {{count} item} other {{count} items}}
Select messages are used for the localization of messages that have multiple variants depending on a variable value. They are useful when you need to choose between two or more message variants based on a certain condition.
# Example with a numbered placeholder
gender = {0, select, male {Mr.} female {Mrs.} other {-}}
# Example with a named placeholder
gender = {gender, select, male {Mr.} female {Mrs.} other {-}}
Comments are used to provide additional information or context about the key-value pairs that are stored in the file. Comments can be added to a properties file using the hash symbol (#
) or the exclamation mark (!
) at the beginning of a line.
# This is a comment
! This is also a comment
Newlines can be used to format localized messages for better readability. A newline can be inserted in a message by using the escape sequence “\n
”.
greeting = Hello!\nHow are you?
In Java Properties files, spaces around the equal sign (=
) are optional. They are a matter of personal taste and do not have any effect on the interpretation of localization messages. However, it is good practice to use a consistent style throughout your localization file for better clarity and maintainability.
# Message example with spaces around the equal sign
greeting = Hello!
# Message example without spaces around the equal sign
greeting=Hello!
If your Java properties files contain escaped Unicode characters (\uXXXX
), it likely indicates that your files are encoded using the ISO 8859-1 (Latin-1) standard, especially if your translations include non-Latin characters. Historically, Java properties files were primarily encoded using ISO 8859-1, and the only method to support non-Latin characters was through the use of escaped Unicode sequences. However, more recent versions of Java have adopted UTF-8 encoding. This change ensures better compatibility and offers a more convenient way to represent non-Latin characters in Java properties files.
# Message example with escaped Unicode character
welcome = \u00a1Bienvenidos todos!
While Java Properties files are commonly used for localization in Java applications, they are not limited to the Java programming language. In fact, other programming languages can also use Java Properties files for localization.
Java properties files can be prone to several common mistakes, such as syntax errors, duplicate keys, encoding issues, missing values, and unescaped characters.
When you organize your translations into separate files for each language, you facilitate easier management of your localization messages in the future. This approach enables you to navigate more easily to the desired translations in your Java projects, find and update specific translations, and also distribute work more effectively among developers and translators in your team.
Previous: Supported file formats
Read next: Professional Translation Services
Tired of manually editing translation files?
Our platform streamlines software localization for you.