All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.yadaframework.core.YadaLocalePathLinkBuilder Maven / Gradle / Ivy

There is a newer version: 0.7.7.R4
Show newest version
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();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy