com.squarespace.template.MessageFormats Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of template-core Show documentation
Show all versions of template-core Show documentation
Squarespace template compiler
/**
* Copyright (c) 2020 SQUARESPACE, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squarespace.template;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import com.squarespace.cldrengine.CLDR;
import com.squarespace.cldrengine.api.Bundle;
import com.squarespace.cldrengine.api.CalendarDate;
import com.squarespace.cldrengine.api.CurrencyFormatOptions;
import com.squarespace.cldrengine.api.CurrencyType;
import com.squarespace.cldrengine.api.DateFormatOptions;
import com.squarespace.cldrengine.api.DateIntervalFormatOptions;
import com.squarespace.cldrengine.api.Decimal;
import com.squarespace.cldrengine.api.DecimalFormatOptions;
import com.squarespace.cldrengine.api.MessageArgConverter;
import com.squarespace.cldrengine.api.MessageFormatFuncMap;
import com.squarespace.cldrengine.api.MessageFormatter;
import com.squarespace.cldrengine.api.MessageFormatterOptions;
import com.squarespace.cldrengine.decimal.DecimalConstants;
import com.squarespace.cldrengine.message.DefaultMessageArgConverter;
/**
* Hooks custom formatting functions into the @phensley/cldr message formatter.
*/
public class MessageFormats {
private static final String DEFAULT_ZONE = "America/New_York";
private final CLDR cldr;
private final MessageArgConverter converter;
private final MessageFormatter formatter;
private String zoneId = DEFAULT_ZONE;
public MessageFormats(CLDR cldr) {
this.cldr = cldr;
Bundle bundle = cldr.General.bundle();
this.converter = new ArgConverter();
MessageFormatterOptions options = MessageFormatterOptions.build()
.cacheSize(100)
.converter(converter)
.formatters(formatters())
.language(bundle.language())
.region(bundle.region());
this.formatter = new MessageFormatter(options);
}
public void setTimeZone(String zoneId) {
this.zoneId = zoneId;
}
public MessageFormatter formatter() {
return this.formatter;
}
private MessageFormatFuncMap formatters() {
MessageFormatFuncMap map = new MessageFormatFuncMap();
map.put("money", this::currency);
map.put("currency", this::currency);
map.put("datetime", this::datetime);
map.put("datetime-interval", this::interval);
map.put("number", this::decimal);
map.put("decimal", this::decimal);
return map;
}
/**
* Currency message formatter.
*/
private String currency(List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy