net.yadaframework.core.YadaLocalePathLinkBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yadaweb Show documentation
Show all versions of yadaweb Show documentation
Some useful tasks for the Yada Framework
package net.yadaframework.core;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import org.springframework.context.i18n.LocaleContextHolder;
import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.linkbuilder.StandardLinkBuilder;
/**
* Needed to create localized urls when using thymeleaf @{} syntax with context-relative paths like @{/somePath}.
* Also need to version the res folder because the yada:href dialect is not called when inlining with [[@{/somePath}]]
* @see YadaLinkBuilder
*/
@Deprecated // Use YadaLinkBuilder instead
public class YadaLocalePathLinkBuilder extends StandardLinkBuilder {
private Pattern nonLocalizedUrls;
public YadaLocalePathLinkBuilder(String notLocalizedResourcePattern) {
super();
nonLocalizedUrls = Pattern.compile(notLocalizedResourcePattern);
}
@Override
protected String computeContextPath(IExpressionContext context, String base, Map parameters) {
StringBuilder result = new StringBuilder(super.computeContextPath(context, base, parameters));
// Adding the current locale, but only if it is not a resource
boolean localizable = !nonLocalizedUrls.matcher(base).find();
if (localizable) {
Locale locale = LocaleContextHolder.getLocale();
String localeString = locale.getLanguage();
result.append("/").append(localeString);
}
return result.toString();
}
}