X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fwotns%2Ffreenet%2FL10nFilter.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fwotns%2Ffreenet%2FL10nFilter.java;h=692b572d70d1d73579594fce577d2cd8e20dcd39;hb=622c4a4d3ebed447d5708a41cf3e1e82e18fa29b;hp=0000000000000000000000000000000000000000;hpb=77869390c46e8e5eff63bf00c7ef44a37ba8f317;p=WoTNS.git diff --git a/src/main/java/net/pterodactylus/wotns/freenet/L10nFilter.java b/src/main/java/net/pterodactylus/wotns/freenet/L10nFilter.java new file mode 100644 index 0000000..692b572 --- /dev/null +++ b/src/main/java/net/pterodactylus/wotns/freenet/L10nFilter.java @@ -0,0 +1,73 @@ +/* + * Sone - L10nFilter.java - Copyright © 2010 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.wotns.freenet; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import net.pterodactylus.util.template.Filter; +import net.pterodactylus.util.template.TemplateContext; +import freenet.l10n.BaseL10n; + +/** + * {@link Filter} implementation replaces {@link String} values with their + * translated equivalents. + * + * @author David ‘Bombe’ Roden + */ +public class L10nFilter implements Filter { + + /** The l10n handler. */ + private final BaseL10n l10n; + + /** + * Creates a new L10n filter. + * + * @param l10n + * The l10n handler + */ + public L10nFilter(BaseL10n l10n) { + this.l10n = l10n; + } + + /** + * {@inheritDoc} + */ + @Override + public String format(TemplateContext templateContext, Object data, Map parameters) { + if (parameters.isEmpty()) { + return l10n.getString(String.valueOf(data)); + } + List parameterValues = new ArrayList(); + int parameterIndex = 0; + while (parameters.containsKey(String.valueOf(parameterIndex))) { + Object value = parameters.get(String.valueOf(parameterIndex)); + if (((String) value).startsWith("=")) { + value = ((String) value).substring(1); + } else { + value = templateContext.get((String) value); + } + parameterValues.add(value); + ++parameterIndex; + } + return new MessageFormat(l10n.getString(String.valueOf(data)), new Locale(l10n.getSelectedLanguage().shortCode)).format(parameterValues.toArray()); + } +}