|
38 | 38 | import org.broadleafcommerce.common.i18n.domain.TranslatedEntity;
|
39 | 39 | import org.broadleafcommerce.common.i18n.domain.Translation;
|
40 | 40 | import org.broadleafcommerce.common.i18n.domain.TranslationImpl;
|
| 41 | +import org.broadleafcommerce.common.locale.service.LocaleService; |
| 42 | +import org.broadleafcommerce.common.locale.util.LocaleUtil; |
41 | 43 | import org.broadleafcommerce.common.sandbox.SandBoxHelper;
|
42 | 44 | import org.broadleafcommerce.common.web.BroadleafRequestContext;
|
43 | 45 | import org.springframework.beans.factory.annotation.Value;
|
44 | 46 | import org.springframework.stereotype.Service;
|
45 | 47 | import org.springframework.transaction.annotation.Transactional;
|
46 | 48 |
|
| 49 | +import java.util.ArrayList; |
47 | 50 | import java.util.HashMap;
|
48 | 51 | import java.util.List;
|
49 | 52 | import java.util.Locale;
|
@@ -76,6 +79,15 @@ public class TranslationServiceImpl implements TranslationService {
|
76 | 79 | @Value("${translation.thresholdForFullCache:1000}")
|
77 | 80 | protected int thresholdForFullCache;
|
78 | 81 |
|
| 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 | + |
79 | 91 | @Resource(name="blGenericEntityDao")
|
80 | 92 | protected GenericEntityDao genericEntityDao;
|
81 | 93 |
|
@@ -412,4 +424,55 @@ protected int getThresholdForFullCache() {
|
412 | 424 | }
|
413 | 425 | }
|
414 | 426 |
|
| 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 | + |
415 | 478 | }
|
0 commit comments