X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2FL10nFilter.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2FL10nFilter.java;h=0000000000000000000000000000000000000000;hp=d667811d558b02cdc083515d9122920fabf4c80a;hb=d50730f6a330439e0e7ef97ca9329dffe72d5640;hpb=97fe04482ebb8a08e43294acde041c2975cbd8ee diff --git a/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java b/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java deleted file mode 100644 index d667811..0000000 --- a/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Sone - L10nFilter.java - Copyright © 2010–2019 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.sone.freenet; - -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; -import java.util.Map; - -import javax.annotation.Nonnull; - -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. - */ -public class L10nFilter implements Filter { - - private final BaseL10n l10n; - - public L10nFilter(BaseL10n l10n) { - this.l10n = l10n; - } - - /** - * {@inheritDoc} - */ - @Override - public String format(TemplateContext templateContext, Object data, Map parameters) { - List parameterValues = getParameters(data, parameters); - String text = getText(data); - if (parameterValues.isEmpty()) { - return l10n.getString(text); - } - return new MessageFormat(l10n.getString(text), new Locale(l10n.getSelectedLanguage().shortCode)).format(parameterValues.toArray()); - } - - @Nonnull - private String getText(Object data) { - return (data instanceof L10nText) ? ((L10nText) data).getText() : String.valueOf(data); - } - - @Nonnull - private List getParameters(Object data, Map parameters) { - if (data instanceof L10nText) { - return ((L10nText) data).getParameters(); - } - List parameterValues = new ArrayList<>(); - int parameterIndex = 0; - while (parameters.containsKey(String.valueOf(parameterIndex))) { - Object value = parameters.get(String.valueOf(parameterIndex)); - parameterValues.add(value); - ++parameterIndex; - } - return parameterValues; - } - -}