name: manage-localization-advanced description: Manage advanced localization in OFBiz, including date/time formatting, currency conversion, and locale-specific data handling beyond simple labels.
Manage Localization Advanced Skill
This skill covers localization techniques in OFBiz that go beyond standard UI labels, focusing on data formatting and locale-aware logic.
Date and Time Localization
Use UtilDateTime for date arithmetic and locale-specific date retrieval.
Pattern: Formatting Dates
// Format date for specific locale and timezone
String formattedDate = UtilFormatOut.formatDate(myDate, "yyyy-MM-dd", locale, timeZone);
// Format date-time for specific locale and timezone
String formattedDateTime = UtilFormatOut.formatDateTime(myTimestamp, "yyyy-MM-dd HH:mm", locale, timeZone);
Pattern: Date Arithmetic
// Get start of month for a timestamp
Timestamp monthStart = UtilDateTime.getMonthStart(nowTimestamp, timeZone, locale);
// Add days to timestamp
Timestamp weekLater = UtilDateTime.addDaysToTimestamp(nowTimestamp, 7);
Currency and Number Formatting
Use UtilFormatOut and UtilNumber for currency and numeric formatting.
Pattern: Formatting Currency
// Format currency based on ISO code and locale
String priceStr = UtilFormatOut.formatCurrency(priceBigDecimal, "USD", locale);
// Format percentage
String percentStr = UtilFormatOut.formatPercentage(discountDouble);
UOM (Unit of Measure) System
OFBiz uses the Uom and UomConversion entities for units of measure and currency conversion.
Pattern: Currency Conversion
// Convert currency using standard service
Map<String, Object> results = dispatcher.runSync("convertUom", UtilMisc.toMap(
"uomId", "USD",
"uomIdTo", "EUR",
"originalValue", priceAmount
));
BigDecimal convertedValue = (BigDecimal) results.get("convertedValue");
Localized Data Storage
For properties stored in the database (e.g., system settings), use EntityUtilProperties.
Pattern: Localized DB Properties
<!-- In SystemProperty.xml -->
<SystemProperty systemResourceId="general" systemPropertyId="currency.uom.id.default" systemPropertyValue="USD"/>
// Retrieve localized property from DB
String currencyId = EntityUtilProperties.getPropertyValue("general", "currency.uom.id.default", delegator);
Best Practices
- Always Pass Locale/TimeZone: Never rely on
Locale.getDefault()orTimeZone.getDefault()in multi-user web applications; always use the user's session locale and timezone. - Use UOM Entities: Prefer the
Uomsystem for dimensions, weights, and currencies rather than hardcoding units. - Handle GMT: Use
UtilDateTime.toGmtTimestampString()for logging or external API integrations that require a standard time representation. - Prefer UtilFormatOut: Use the utility classes for formatting to ensure consistency across the application.
Source: apache/ofbiz-plugins — distributed by TomeVault.