Translate for your Language
If you have finished a translation for a new language or dialect that PrettyTime does not currently support, we would love to add it to our supported translations. Just fork our repository on github, and send a pull request with your translation, and a test-case.
Get started
If you are already familiar with the Java resource bundle API, then you can skip this section and dig right in; otherwise, thejava.util.ResourceBundle
API at its most basic level, is a strategy for loading different files or classes based on the current or requested system java.util.Locale
. To use it, your file must end with XX_yy
, where `XX` is the primary Country code, and `yy` is the secodary dialect or region code.
PrettyTime bundles by example
Now, lets look at part of the translation file for the English language. Notice that we are using the ResourceBundle API to load java Classes by Locale, not text property files. This is important because it is possible for ResourceBundles to implement several interfaces, allowing further customization of behavior for features like: custom pluralization and grammar rules, and more.In this and the next sections, we’ll inspect the following resource bundle, and explain each entry in detail.
package org.ocpsoft.prettytime.i18n; import java.util.ListResourceBundle; public class Resources extends ListResourceBundle { private static final Object[][] OBJECTS = new Object[][] { ... { "DayPattern", "%n %u" }, { "DayFuturePrefix", "" }, { "DayFutureSuffix", " from now" }, { "DayPastPrefix", "" }, { "DayPastSuffix", " ago" }, { "DayName", "day" }, { "DayPluralName", "days" }, ... @Override public Object[][] getContents() { return OBJECTS; } }
Contents of the resource bundle
public Object[][] getContents()
This is the method that must return the contents of the resource bundle, including all patterns, names, prefixes, and suffixes.
*FuturePrefix
The grammatically correct word or phrase that one would use before the quantity in a formatted date occurring in the future. For instance, if the fully formatted result is “in 3 days from now”, the future prefix would be “in”.*FutureSuffix
– The grammatically correct word or phrase that one would use after the quantity in a formatted date occurring in the future. For instance, if the fully formatted result is “in 3 days from now”, the future prefix would be “from now”.*PastPrefix
– The grammatically correct word or phrase that one would use before the quantity in a formatted date occurring in the past. For instance, if the fully formatted result is “before 3 days ago”, the future prefix would be “before”.*PastSuffix
– The grammatically correct word or phrase that one would use after the quantity in a formatted date occurring in the past. For instance, if the fully formatted result is “before 3 days ago”, the future prefix would be “ago”.*SingularName
– The grammatically correct word or phrase that describing the singular quantity of a given time unit. For instance, if the fully formatted result is “before 1 day ago”, the singular name would be “day”.*PluralName
– The grammatically correct word or phrase that describing the plural quantity of a given time unit. For instance, if the fully formatted result is “in 2 days, the plural name would be “days”.*FutureSingularName
– (optional) OverridesSingularName
if the date to format is in the future. The grammatically correct word or phrase that describing the singular quantity of a given time unit. For instance, if the fully formatted result is “in 1 day”, the future singular name would be “day”.*FuturePluralName
– (optional) OverridesPluralName
if the date to format is in the future. The grammatically correct word or phrase that describing the plural quantity of a given time unit. For instance, if the fully formatted result is “in 2 days”, the future plural name would be “days”.*PastSingularName
– (optional) OverridesSingularName
if the date to format is in the past. The grammatically correct word or phrase that describing the singular quantity of a given time unit. For instance, if the fully formatted result is “before 1 day ago”, the past singular name would be “day”.*PastPluralName
– (optional) OverridesPluralName
if the date to format is in the past. The grammatically correct word or phrase that describing the plural quantity of a given time unit. For instance, if the fully formatted result is “in 2 days, the past plural name would be “days”.*Pattern
– In the example above, the bundle contains an entry for"DayPattern", "%n %u"
. This entry is responsible for setting the pattern with which a given time unit should be formatted. You can control ordering and formatting for each time unit by manipulating the pattern.
{prefix} {pattern} {suffix}
– where {pattern} is created through the following codes:
%n = number of units (the actual number that will show up) %u = name of the unit (either singular or plural, depending on run time conditions)
Most languages (using the standard%n %u
pattern) will render something like:{prefix} {number} {unitName} {suffix}
. There are two exceptions to the general rule. The “Just Now” unit, doesn’t use a number; simply “moments from now” or “moments ago”. The “Abstract Time” block is currently unused, so keep it blank. You probably want to keep the default pattern%n %u
unless your language needs something different.
You could also do something wacky like:This would effectively printExhibit 1public class Resources extends ListResourceBundle { private static final Object[][] OBJECTS = new Object[][] { ... { "DayPattern", "%n freakin' %u" } ... }
5 freakin' days ago
. Fun!
[…] Translate […]