Multiple company locale java (localize property files and resourcess – css,images)

Posted: July 2, 2013 in web

On netbeans, project struct looks similar to this.

new-project
web pages
web-inf
faces-config.xml
resources
companyA
css
main.css
companyB
css
main.css
source packages
com.company.locale.test
TestLocalFilter.java
TestLocaleWrapper.java
Other Sources
com.company.locale.test
content_en_us.properties
content_en_us_companyA.properties
content_en_us_companyA_tx.properties
content_en_us_companyA_ca.properties
content_en_us_companyB.properties
content_en_us_companyB_tx.properties
content_en_us_companyB_ca.properties

index.xhtml

<h:head>
 <title>Facelet Title</title>
 <h:outputStylesheet library="#{content['library.css']}" name="main.css" />
 </h:head>
 <h:body>
 <h:outputText value="#{content['test']}" />
 </h:body>

faces-config.xml

<application>
 <resource-bundle>
 <base-name>com.company.locale.test.content</base-name>
 <var>content</var>
 </resource-bundle>

 <locale-config>
 <default-locale>en_US</default-locale>
 <supported-locale>en_US_companyA</supported-locale>
 <supported-locale>en_US_companyA_TX</supported-locale>
 <supported-locale>en_US_companyA_CA</supported-locale>
 <supported-locale>en_US_companyB</supported-locale>
 <supported-locale>en_US_companyB_TX</supported-locale>
 <supported-locale>en_US_companyB_CA</supported-locale>
 </locale-config>
 </application>

TestLocalFilter.java

@WebFilter(filterName = "TestLocaleFilter", urlPatterns = {"/*"})
public class LocaleFilter implements Filter {

@Override
 public void doFilter(ServletRequest request, ServletResponse response,
 FilterChain chain)
 throws IOException, ServletException {

 Locale locale = new Locale("en", "US", "companyA");
 String host = request.getServerName();
 if (host.contains("companyB")) {
 locale = new Locale("en", "US", "companyB");
 }

 Config.set(request, Config.FMT_LOCALE, locale);
 request = new LocaleRequestWrapper((HttpServletRequest) request, locale);
 chain.doFilter(request, response);
 }

 @Override
 public void init(FilterConfig filterConfig) throws ServletException {
 }

 @Override
 public void destroy() {
 }
}

TestLocaleWrapper.java

public class TestLocaleWrapper extends HttpServletRequestWrapper {
 private final Locale preferredLocale;

public TestLocaleWrapper(final HttpServletRequest decorated, final Locale userLocale) {
 super(decorated);
 preferredLocale = userLocale;
 }

@Override
 public Locale getLocale() {
 return (preferredLocale != null ? preferredLocale : super.getLocale());
 }

@Override
 public Enumeration<Locale> getLocales() {
 if (preferredLocale != null) {
 List<Locale> locales = Collections.list(super.getLocales());
 if (locales.contains(preferredLocale)) {
 locales.remove(preferredLocale);
 }
 locales.add(0, preferredLocale);
 return Collections.enumeration(locales);
 } else {
 return super.getLocales();
 }
 }
}

content_en_US_companyA.properties

test=content_en_us_iei
library.css=companyA/css
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s