Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions sitebricks/src/main/java/com/google/sitebricks/Localizer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.google.sitebricks;

import com.google.sitebricks.locale.LocaleProvider;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -40,7 +41,7 @@ public class Localizer {

/**
* A value object that represents the localization of an i18n interface to a locale
* and corresponding set of messages.
* and corresponding set of messages.
*/
public static class Localization {
// TODO(dhanji): Convert class reference to weak?
Expand All @@ -53,7 +54,7 @@ public Localization(Class<?> clazz, Locale locale, Map<String, String> messageBu
this.locale = locale;
this.messageBundle = messageBundle;
}

public Class<?> getClazz() {
return this.clazz;
}
Expand Down Expand Up @@ -168,7 +169,7 @@ private void bindMessageProvider(final Class<?> iface,

// Wonderful Guice hack to get around not using assisted inject.
@Inject
private final Provider<HttpServletRequest> requestProvider = null;
final LocaleProvider localeProvider = null;

// This is our delegate field that proxies the interface.
private final Object instance = Proxy.newProxyInstance(
Expand All @@ -179,7 +180,7 @@ private void bindMessageProvider(final Class<?> iface,
* Returns the localized message bundle value, keyed by the method name invoked.
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Locale locale = requestProvider.get().getLocale();
Locale locale = localeProvider.getLocale();
Map<String, MessageDescriptor> messages = getMessagesWithFallback(locale);

// Use default if we don't support the given locale.
Expand All @@ -189,7 +190,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl

MessageDescriptor descriptor = messages.get(method.getName());
if (descriptor == null) {
throw new IllegalStateException("Could not find message '"
throw new IllegalStateException("Could not find message '"
+ method.getName() + "' in " + messages);
}
return descriptor.render(args);
Expand All @@ -199,7 +200,7 @@ private Map<String, MessageDescriptor> getMessagesWithFallback(Locale locale) {
String localeInterfaceKey = createLocaleInterfaceKey(iface, locale);
Map<String, MessageDescriptor> result = localizedValues.get(localeInterfaceKey);
if (result == null) {
result = localizedValues.get(new Locale(locale.getLanguage()));
result = localizedValues.get(createLocaleInterfaceKey(iface, new Locale(locale.getLanguage())));
}
return result;
}
Expand All @@ -215,7 +216,7 @@ public Object get() {

}

private String createLocaleInterfaceKey(final Class<?> iface, Locale locale) {
private String createLocaleInterfaceKey(final Class<?> iface, Locale locale) {
return locale.toString() + ":" + iface.getName();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.google.sitebricks;

import com.google.sitebricks.locale.LocaleProviderModule;
import java.lang.annotation.Annotation;
import java.util.Enumeration;
import java.util.List;
Expand Down Expand Up @@ -120,11 +121,20 @@ protected final void configure() {

configureTemplateSystem();

/* Now bind the locale provider.*/
bindLocaleProvider();
}

/**
* Used to bind the Locale provider. Can be overwritten if custom Locale behavior is desider.
*/
protected void bindLocaleProvider() {
install(new LocaleProviderModule());
}

protected void configureTemplateSystem() {
//
// Map of all the implementations keyed by type they can handle
// Map of all the implementations keyed by type they can handle
//
ImmutableMap.Builder<String, Class<? extends TemplateCompiler>> builder = ImmutableMap.builder();

Expand Down Expand Up @@ -232,7 +242,7 @@ public void using(Locale locale, ResourceBundle bundle) {
public void usingDefault() {
add(Localizer.defaultLocalizationFor(iface));
}

};

}
Expand All @@ -246,7 +256,7 @@ private void add(Localizer.Localization localization) {
}
localeLocalizer.put(localization.getLocale(), localization);
}

protected final void scan(Package pack) {
Preconditions.checkArgument(null != pack, "Package parameter to scan() cannot be null");
packages.add(pack);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.google.sitebricks.locale;

import java.util.Locale;

/**
* Provides the {@link java.util.Locale} for the internationalization.
*/
public interface LocaleProvider {

/**
* Retrieves the locale that is to be used for the i18n of the translatable messages.
*
* @return the requested {@link java.util.Locale}.
*/
Locale getLocale();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.google.sitebricks.locale;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import com.google.inject.Inject;
import com.google.inject.Provider;

/**
* Provides a default implementation of the {@link LocaleProvider}. It retrieves the Locale as stored in the
* {@link javax.servlet.http.HttpServletRequest}.
*/
class LocaleProviderImpl implements LocaleProvider {

private final Provider<HttpServletRequest> requestProvider;

@Inject
LocaleProviderImpl(final Provider<HttpServletRequest> requestProvider) {
this.requestProvider = requestProvider;
}

/**
* @return the Locale as stored in the {@link javax.servlet.http.HttpServletRequest}.
*/
public Locale getLocale() {
if (requestProvider == null) {
throw new IllegalStateException("The HttpServletRequest provider must be bound.");
}

final HttpServletRequest request = requestProvider.get();
if (request == null) {
throw new IllegalStateException("No HttpServletRequest could be retrieved. Cannot determine user locale.");
}

return request.getLocale();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.google.sitebricks.locale;

import com.google.inject.AbstractModule;
import com.google.inject.Singleton;

public class LocaleProviderModule extends AbstractModule {
@Override
protected final void configure() {
bind(LocaleProvider.class).to(LocaleProviderImpl.class).in(Singleton.class);
}
}
Loading