Java Properties file (.properties)

 

Java Properties (.properties) files are commonly used to store localization messages for Java applications. They are simple text files that consist of key-value pairs. The key represents the message identifier and the value represents the corresponding translation.

These files can be encoded using different formats, such as UTF-8 or Latin1. To ensure better compatibility and a wider range of characters, it is recommended to use UTF-8 encoding when creating Java Properties files in newer versions of Java (Java 9 and later).

Note: The way localization messages are handled can vary between different localization libraries. The examples provided here should be compatible with the icu4j library.

Java Properties file example

! 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

Placeholder

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.

greeting = Hello, {0}!
greeting = Hello, {name}!

Plural

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.

counter = {0, plural, one {{0} item} other {{0} items}}
counter = {count, plural, one {{count} item} other {{count} items}}

Select

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.

gender = {0, select, male {Mr.} female {Mrs.} other {-}}
gender = {gender, select, male {Mr.} female {Mrs.} other {-}}

Comment

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

Newline

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?

Do other programming languages use Java properties for localization files?

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.

What are the most frequent mistakes in Java properties files?

Java properties files can be prone to several common mistakes, such as syntax errors, duplicate keys, encoding issues, missing values, and unescaped characters.

How can storing translations for each language in separate Java properties files improve localization management?

Storing translations in separate Java properties files simplifies localization management. It enables easy organization and updates for each language, and helps collaborators focus on specific files. It also simplifies version control and speeds up the localization process.

Tired of manually editing translation files?

Our platform streamlines software localization for you.

Copyrights 2024 © Localizely