use of org.broadleafcommerce.common.i18n.domain.Translation in project BroadleafCommerce by BroadleafCommerce.
the class TranslationServiceImpl method getTranslatedValue.
@Override
public String getTranslatedValue(Object entity, String property, Locale locale) {
TranslatedEntity entityType = getEntityType(entity);
String entityId = dao.getEntityId(entityType, entity);
String localeCode = locale.getLanguage();
String localeCountryCode = localeCode;
if (StringUtils.isNotBlank(locale.getCountry())) {
localeCountryCode += "_" + locale.getCountry();
}
if (TranslationBatchReadCache.hasCache()) {
Translation translation = TranslationBatchReadCache.getFromCache(entityType, entityId, property, localeCountryCode);
if (translation != null) {
return translation.getTranslatedValue();
} else {
// There is no translation for this entity if it is not in the cache
return null;
}
}
boolean isValidForCache = false;
if (extensionManager != null) {
ExtensionResultHolder<Boolean> response = new ExtensionResultHolder<Boolean>();
response.setResult(false);
extensionManager.getProxy().isValidState(response);
isValidForCache = response.getResult();
}
if (!BroadleafRequestContext.getBroadleafRequestContext().isProductionSandBox() || !isValidForCache) {
Translation translation = dao.readTranslation(entityType, entityId, property, localeCode, localeCountryCode, ResultType.CATALOG_ONLY);
if (translation != null) {
return translation.getTranslatedValue();
} else {
return null;
}
}
return getOverrideTranslatedValue(property, entityType, entityId, localeCode, localeCountryCode);
}
use of org.broadleafcommerce.common.i18n.domain.Translation in project BroadleafCommerce by BroadleafCommerce.
the class TranslationServiceImpl method getOverrideTranslatedValue.
protected String getOverrideTranslatedValue(String property, TranslatedEntity entityType, String entityId, String localeCode, String localeCountryCode) {
boolean specificTranslationDeleted = false;
boolean generalTranslationDeleted = false;
StandardCacheItem specificTranslation = null;
StandardCacheItem generalTranslation = null;
String specificPropertyKey = property + "_" + localeCountryCode;
String generalPropertyKey = property + "_" + localeCode;
String response = null;
String cacheKey = getCacheKey(ResultType.STANDARD, entityType);
LocalePair override = null;
for (TranslationOverrideStrategy strategy : strategies) {
override = strategy.getLocaleBasedOverride(property, entityType, entityId, localeCode, localeCountryCode, cacheKey);
if (override != null) {
specificTranslation = override.getSpecificItem();
generalTranslation = override.getGeneralItem();
break;
}
}
if (override == null) {
throw new IllegalStateException("Expected at least one TranslationOverrideStrategy to return a valid value");
}
if (specificTranslation != null) {
if (ItemStatus.DELETED.equals(specificTranslation.getItemStatus())) {
specificTranslationDeleted = true;
} else {
if (specificTranslation.getCacheItem() instanceof Translation) {
response = ((Translation) specificTranslation.getCacheItem()).getTranslatedValue();
} else {
response = (String) specificTranslation.getCacheItem();
}
return replaceEmptyWithNullResponse(response);
}
}
if (generalTranslation != null) {
if (ItemStatus.DELETED.equals(generalTranslation.getItemStatus())) {
generalTranslationDeleted = true;
// If the general translation is override deleted and we've only got a general local code - we're done
if (specificTranslationDeleted || !localeCountryCode.contains("_")) {
return null;
}
} else {
if (generalTranslation.getCacheItem() instanceof Translation) {
response = ((Translation) generalTranslation.getCacheItem()).getTranslatedValue();
} else {
response = (String) generalTranslation.getCacheItem();
}
// If the general translation is override and we've only got a general local code - we're done
if (specificTranslationDeleted || !localeCountryCode.contains("_")) {
return replaceEmptyWithNullResponse(response);
}
// We have a valid general override - don't check for general template value
generalTranslationDeleted = true;
}
}
// Check for a Template Match
if (specificTranslationDeleted) {
// only check general properties since we explicitly deleted specific properties at standard (site) level
specificPropertyKey = generalPropertyKey;
} else if (generalTranslationDeleted) {
// only check specific properties since we explicitly deleted general properties at standard (site) level
generalPropertyKey = specificPropertyKey;
}
String templateResponse = getTemplateTranslatedValue(cacheKey, property, entityType, entityId, localeCode, localeCountryCode, specificPropertyKey, generalPropertyKey);
if (templateResponse != null) {
response = templateResponse;
}
return response;
}
use of org.broadleafcommerce.common.i18n.domain.Translation in project BroadleafCommerce by BroadleafCommerce.
the class TranslationServiceImpl method save.
@Override
@Transactional("blTransactionManager")
public Translation save(String entityType, String entityId, String fieldName, String localeCode, String translatedValue) {
TranslatedEntity te = getEntityType(entityType);
Translation translation = getTranslation(te, entityId, fieldName, localeCode);
if (translation == null) {
translation = dao.create();
translation.setEntityType(te);
translation.setEntityId(entityId);
translation.setFieldName(fieldName);
translation.setLocaleCode(localeCode);
}
translation.setTranslatedValue(translatedValue);
return save(translation);
}
use of org.broadleafcommerce.common.i18n.domain.Translation in project BroadleafCommerce by BroadleafCommerce.
the class TranslationDaoImpl method readTranslation.
@Override
public Translation readTranslation(TranslatedEntity entityType, String entityId, String fieldName, String localeCode, String localeCountryCode, ResultType stage) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
Root<TranslationImpl> root = criteria.from(TranslationImpl.class);
criteria.select(root);
List<Predicate> restrictions = new ArrayList<Predicate>();
restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType()));
restrictions.add(builder.equal(root.get("entityId"), entityId));
restrictions.add(builder.equal(root.get("fieldName"), fieldName));
restrictions.add(builder.like(root.get("localeCode").as(String.class), localeCode + "%"));
try {
if (extensionManager != null) {
extensionManager.getProxy().setup(TranslationImpl.class, stage);
extensionManager.getProxy().refineParameterRetrieve(TranslationImpl.class, stage, builder, criteria, root, restrictions);
}
criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
TypedQuery<Translation> query = em.createQuery(criteria);
if (extensionManager != null) {
extensionManager.getProxy().refineQuery(TranslationImpl.class, stage, query);
}
query.setHint(QueryHints.HINT_CACHEABLE, true);
List<Translation> translations = query.getResultList();
if (!translations.isEmpty()) {
if (!localeCode.equals(localeCountryCode)) {
return findBestTranslation(localeCountryCode, translations);
} else {
return findSpecificTranslation(localeCountryCode, translations);
}
} else {
return null;
}
} finally {
if (extensionManager != null) {
extensionManager.getProxy().breakdown(TranslationImpl.class, stage);
}
}
}
use of org.broadleafcommerce.common.i18n.domain.Translation in project BroadleafCommerce by BroadleafCommerce.
the class TranslationFormBuilderServiceImpl method buildListGrid.
@Override
public ListGrid buildListGrid(List<Translation> translations, boolean isRte) {
// Set up the two header fields we're interested in for the translations list grid
List<Field> headerFields = new ArrayList<Field>();
headerFields.add(new Field().withName("localeCode").withFriendlyName("Translation_localeCode").withOrder(0));
headerFields.add(new Field().withName("translatedValue").withFriendlyName("Translation_translatedValue").withOrder(10));
// Create the list grid and set its basic properties
ListGrid listGrid = new ListGrid();
listGrid.getHeaderFields().addAll(headerFields);
listGrid.setListGridType(ListGrid.Type.TRANSLATION);
listGrid.setSelectType(ListGrid.SelectType.SINGLE_SELECT);
listGrid.setCanFilterAndSort(false);
// Allow add/update/remove actions, but provisioned especially for translation. Because of this, we will clone
// the default actions so that we may change the class
ListGridAction addAction = DefaultListGridActions.ADD.clone();
ListGridAction removeAction = DefaultListGridActions.REMOVE.clone();
ListGridAction updateAction = DefaultListGridActions.UPDATE.clone();
addAction.setButtonClass("translation-grid-add");
removeAction.setButtonClass("translation-grid-remove");
updateAction.setButtonClass("translation-grid-update");
listGrid.addToolbarAction(addAction);
listGrid.addRowAction(updateAction);
listGrid.addRowAction(removeAction);
// TODO rework code elsewhere so these don't have to be added
listGrid.setSectionKey(Translation.class.getCanonicalName());
listGrid.setSubCollectionFieldName("translation");
// Create records for each of the entries in the translations list
for (Translation t : translations) {
ListGridRecord record = new ListGridRecord();
record.setListGrid(listGrid);
record.setId(String.valueOf(t.getId()));
Locale locale = localeService.findLocaleByCode(t.getLocaleCode());
record.getFields().add(new Field().withName("localeCode").withFriendlyName("Translation_localeCode").withOrder(0).withValue(locale.getLocaleCode()).withDisplayValue(locale.getFriendlyName()));
record.getFields().add(new Field().withName("translatedValue").withFriendlyName("Translation_translatedValue").withOrder(10).withValue(t.getTranslatedValue()).withDisplayValue(isRte ? getLocalizedEditToViewMessage() : t.getTranslatedValue()));
listGrid.getRecords().add(record);
}
listGrid.setTotalRecords(listGrid.getRecords().size());
return listGrid;
}
Aggregations