com.commercetools.sunrise.common.template.i18n.composite.CompositeI18nResolver Maven / Gradle / Ivy
package com.commercetools.sunrise.common.template.i18n.composite;
import com.commercetools.sunrise.common.template.i18n.I18nIdentifier;
import com.commercetools.sunrise.common.template.i18n.I18nResolver;
import io.sphere.sdk.models.Base;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
/**
* Resolves the i18n messages by trying to resolve them with each given i18n resolver until one of them succeeds.
* This class allows to override i18n resolvers.
*/
public final class CompositeI18nResolver extends Base implements I18nResolver {
private final List i18nResolvers;
private CompositeI18nResolver(final List i18nResolvers) {
this.i18nResolvers = i18nResolvers;
}
@Override
public Optional get(final List locales, final I18nIdentifier i18nIdentifier, final Map args) {
for (I18nResolver i18nResolver : i18nResolvers) {
final Optional message = i18nResolver.get(locales, i18nIdentifier, args);
if (message.isPresent()) {
return message;
}
}
return Optional.empty();
}
public static CompositeI18nResolver of(final List i18nResolvers) {
return new CompositeI18nResolver(i18nResolvers);
}
}