Skip to content

Commit 360c149

Browse files
committed
QA#764 - Add property to not return the default value for a translated field if that value is not in the same language.
Instead, return blank. The new property to drive this behavior is named "returnBlankTranslationForNotDefaultLocale". This ticket also provides an overridable method in TranslationServiceImpl to exclude some fields from this logic. In particular, the CMS lookup fields (which start with pageTemplate) needed to be excluded for the home page to render in heat-clinic. This is because all page template fields currently go through translation provider (even fields like ID) which are not even translatable. Relates to BroadleafCommerce/QA#764
1 parent fb1d73a commit 360c149

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

common/src/main/java/org/broadleafcommerce/common/i18n/service/DynamicTranslationProvider.java

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public static String getValue(Object obj, String field, final String defaultValu
5050

5151
if (StringUtils.isNotBlank(translatedValue)) {
5252
valueToReturn = translatedValue;
53+
} else {
54+
valueToReturn = translationService.getDefaultTranslationValue(obj, field, locale, defaultValue);
5355
}
5456
}
5557

common/src/main/java/org/broadleafcommerce/common/i18n/service/TranslationService.java

+19
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,23 @@ public Translation save(String entityType, String entityId, String fieldName, St
130130
*/
131131
Cache getCache();
132132

133+
/**
134+
* Intended for use with the {@link DynamicTranslationProvider} to determine the default value when a
135+
* translation was not provided.
136+
*
137+
* The default implementation of this method relies on a system property "returnBlankTranslationForNotDefaultLocale".
138+
* If this is true, the system will return blank if the language of the defaultLocale does not match the language
139+
* of the passed in locale.
140+
*
141+
* For example, consider the "longDescription" property and the system default locale is "en". If this method is
142+
* called for a locale of "en_CA", the requestedDefaultValue will be returned. If the method is called with a value
143+
* of "fr_CA", blank will be returned.
144+
*
145+
* @param entity
146+
* @param property
147+
* @param locale
148+
* @param requestedDefaultValue
149+
* @return
150+
*/
151+
String getDefaultTranslationValue(Object entity, String property, Locale locale, String requestedDefaultValue);
133152
}

common/src/main/java/org/broadleafcommerce/common/i18n/service/TranslationServiceImpl.java

+63
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@
3838
import org.broadleafcommerce.common.i18n.domain.TranslatedEntity;
3939
import org.broadleafcommerce.common.i18n.domain.Translation;
4040
import org.broadleafcommerce.common.i18n.domain.TranslationImpl;
41+
import org.broadleafcommerce.common.locale.service.LocaleService;
42+
import org.broadleafcommerce.common.locale.util.LocaleUtil;
4143
import org.broadleafcommerce.common.sandbox.SandBoxHelper;
4244
import org.broadleafcommerce.common.web.BroadleafRequestContext;
4345
import org.springframework.beans.factory.annotation.Value;
4446
import org.springframework.stereotype.Service;
4547
import org.springframework.transaction.annotation.Transactional;
4648

49+
import java.util.ArrayList;
4750
import java.util.HashMap;
4851
import java.util.List;
4952
import java.util.Locale;
@@ -76,6 +79,15 @@ public class TranslationServiceImpl implements TranslationService {
7679
@Value("${translation.thresholdForFullCache:1000}")
7780
protected int thresholdForFullCache;
7881

82+
@Value("${returnBlankTranslationForNotDefaultLocale:false}")
83+
protected boolean returnBlankTranslationForNotDefaultLocale;
84+
85+
@Resource(name = "blTranslationExceptionProperties")
86+
protected List<String> translationExceptionProperties = new ArrayList<String>();
87+
88+
@Resource(name = "blLocaleService")
89+
protected LocaleService localeService;
90+
7991
@Resource(name="blGenericEntityDao")
8092
protected GenericEntityDao genericEntityDao;
8193

@@ -412,4 +424,55 @@ protected int getThresholdForFullCache() {
412424
}
413425
}
414426

427+
public String getDefaultTranslationValue(Object entity, String property, Locale locale,
428+
String requestedDefaultValue) {
429+
430+
if (returnBlankTranslationForNotDefaultLocale) {
431+
if (localeMatchesDefaultLocale(locale)) {
432+
} else if (!propertyInDefaultLocaleExceptionList(entity, property)) {
433+
return "";
434+
}
435+
}
436+
437+
return requestedDefaultValue;
438+
}
439+
440+
/**
441+
* Returns true if the passed in entity / property combination is in the defaultLocaleExceptionList
442+
*
443+
* The default implementation checks the "translationExceptionProperties" list to see if the
444+
* property matches one of the regularExpressions in that list.
445+
*
446+
* Implementors are expected to override this method for implementation specific needs.
447+
*
448+
* @param entity
449+
* @param property
450+
* @return
451+
*/
452+
protected boolean propertyInDefaultLocaleExceptionList(Object entity, String property) {
453+
TranslatedEntity entityType = getEntityType(entity);
454+
if (entityType != null && entityType.getFriendlyType() != null) {
455+
for (String exceptionProperty : translationExceptionProperties) {
456+
if (property.matches(exceptionProperty)) {
457+
return true;
458+
}
459+
}
460+
}
461+
return false;
462+
}
463+
464+
/**
465+
* Returns true if the passed in locale's language matches the Broadleaf default locale.
466+
* @param locale
467+
* @return
468+
*/
469+
protected boolean localeMatchesDefaultLocale(Locale locale) {
470+
String defaultLanguage = LocaleUtil.findLanguageCode(localeService.findDefaultLocale());
471+
472+
if (defaultLanguage != null && locale != null) {
473+
return defaultLanguage.equals(locale.getLanguage());
474+
}
475+
return false;
476+
}
477+
415478
}

common/src/main/resources/bl-common-applicationContext.xml

+8
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,12 @@
377377
</map>
378378
</property>
379379
</bean>
380+
381+
<bean id="blTranslationExceptionProperties" class="org.springframework.beans.factory.config.ListFactoryBean" >
382+
<property name="sourceList">
383+
<list>
384+
<value>pageTemplate.*</value>
385+
</list>
386+
</property>
387+
</bean>
380388
</beans>

0 commit comments

Comments
 (0)