use of com.hccake.ballcat.common.i18n.I18nMessage in project ballcat by ballcat-projects.
the class CustomI18nMessageProvider method getI18nMessage.
@Override
public I18nMessage getI18nMessage(String code, Locale locale) {
String languageTag = locale.toLanguageTag();
// 缓存 key
String cacheKey = getCacheKey(code, languageTag);
I18nMessage i18nMessage = cache.get(cacheKey);
// 如果缓存没有,则查询数据库(这里数据库查询其实也加了 redis 缓存)
if (i18nMessage == null) {
I18nData i18nData = i18nDataService.getByCodeAndLanguageTag(code, languageTag);
if (i18nData != null) {
i18nMessage = converterToI18nMessage(i18nData);
// 错峰过期
int timeout = RandomUtil.randomInt(MIN_TIMEOUT, MAX_TIMEOUT);
cache.put(cacheKey, i18nMessage, timeout);
}
}
return i18nMessage;
}
use of com.hccake.ballcat.common.i18n.I18nMessage in project ballcat by ballcat-projects.
the class SysMenuServiceImpl method create.
@Override
@Transactional(rollbackFor = Exception.class)
public boolean create(SysMenuCreateDTO sysMenuCreateDTO) {
SysMenu sysMenu = SysMenuConverter.INSTANCE.createDtoToPo(sysMenuCreateDTO);
Integer menuId = sysMenu.getId();
SysMenu existingMenu = baseMapper.selectById(menuId);
if (existingMenu != null) {
String errorMessage = String.format("ID [%s] 已被菜单 [%s] 使用,请更换其他菜单ID", menuId, existingMenu.getTitle());
throw new BusinessException(BaseResultCode.LOGIC_CHECK_ERROR.getCode(), errorMessage);
}
boolean saveSuccess = SqlHelper.retBool(baseMapper.insert(sysMenu));
Assert.isTrue(saveSuccess, () -> {
log.error("[create] 创建菜单失败,sysMenuCreateDTO: {}", sysMenuCreateDTO);
return new BusinessException(BaseResultCode.UPDATE_DATABASE_ERROR.getCode(), "创建菜单失败");
});
// 多语言保存事件发布
List<I18nMessage> i18nMessages = sysMenuCreateDTO.getI18nMessages();
if (CollectionUtil.isNotEmpty(i18nMessages)) {
eventPublisher.publishEvent(new I18nMessageCreateEvent(i18nMessages));
}
return saveSuccess;
}
use of com.hccake.ballcat.common.i18n.I18nMessage in project ballcat by ballcat-projects.
the class CustomI18nMessageProvider method converterToI18nMessage.
/**
* 转换 i18nData to I18nMessage
* @param i18nData 数据库存储对象
* @return I18nMessage
*/
private I18nMessage converterToI18nMessage(I18nData i18nData) {
I18nMessage i18nMessage = new I18nMessage();
i18nMessage.setMessage(i18nData.getMessage());
i18nMessage.setCode(i18nData.getCode());
i18nMessage.setLanguageTag(i18nData.getLanguageTag());
return i18nMessage;
}
Aggregations