Commit 6ac9343a authored by Matija Obreza's avatar Matija Obreza
Browse files

Load articles from default language if necessary

parent 8f51aae1
......@@ -41,7 +41,7 @@ public interface ContentService {
* @param locale
* @return
*/
Article getArticle(Class<?> clazz, Long id, String slug, Locale locale);
Article getArticle(Class<?> clazz, Long id, String slug, Locale locale, boolean useDefault);
Article getArticle(EntityId entity, String slug, Locale locale);
......@@ -50,9 +50,21 @@ public interface ContentService {
*
* @param slug
* @param locale
* @param useDefault
* Load article from default language
* @return
*/
Article getGlobalArticle(String slug, Locale locale);
Article getGlobalArticle(String slug, Locale locale, boolean useDefault);
/**
* Loads a global article in the specified locale, or when not found the
* default locale
*
* @param string
* @param locale
* @return
*/
Article getGlobalArticle(String string, Locale locale);
Page<Article> listArticles(Pageable pageable);
......@@ -81,4 +93,10 @@ public interface ContentService {
void deleteActivityPost(long id);
/**
* Returns default locale
*
* @return
*/
Locale getDefaultLocale();
}
......@@ -57,6 +57,11 @@ public class ContentServiceImpl implements ContentService {
@Autowired
private HtmlSanitizer htmlSanitizer;
@Override
public Locale getDefaultLocale() {
return Locale.getDefault();
}
@Override
public List<ActivityPost> lastNews() {
PageRequest page = new PageRequest(0, 10, Direction.DESC, "postDate");
......@@ -74,19 +79,31 @@ public class ContentServiceImpl implements ContentService {
articleRepository.save(articles);
}
@Override
public Article getGlobalArticle(String slug, Locale locale, boolean useDefault) {
return getArticle(Article.class, null, slug, locale, useDefault);
}
/**
* Get article, use default locale if required
*/
@Override
public Article getGlobalArticle(String slug, Locale locale) {
return getArticle(Article.class, null, slug, locale);
return getGlobalArticle(slug, locale, true);
}
@Override
public Article getArticle(EntityId entity, String slug, Locale locale) {
return getArticle(entity.getClass(), entity.getId(), slug, locale);
return getArticle(entity.getClass(), entity.getId(), slug, locale, true);
}
@Override
public Article getArticle(Class<?> clazz, Long id, String slug, Locale locale) {
return articleRepository.findByClassPkAndTargetIdAndSlugAndLang(getClassPk(clazz), id, slug, locale.getLanguage());
public Article getArticle(Class<?> clazz, Long id, String slug, Locale locale, boolean useDefault) {
Article article = articleRepository.findByClassPkAndTargetIdAndSlugAndLang(getClassPk(clazz), id, slug, locale.getLanguage());
if (article == null && useDefault && !locale.getLanguage().equals(getDefaultLocale().getLanguage())) {
article = articleRepository.findByClassPkAndTargetIdAndSlugAndLang(getClassPk(clazz), id, slug, getDefaultLocale().getLanguage());
}
return article;
}
@Override
......@@ -147,7 +164,7 @@ public class ContentServiceImpl implements ContentService {
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public Article updateArticle(Class<?> clazz, Long id, String slug, String title, String body, Locale locale) {
Article article = getArticle(clazz, id, slug, locale);
Article article = getArticle(clazz, id, slug, locale, false);
if (article == null || !article.getLang().equals(locale.getLanguage())) {
article = new Article();
article.setClassPk(ensureClassPK(clazz));
......
......@@ -57,7 +57,7 @@ public class ArticleController extends BaseController {
public String edit(ModelMap model, @PathVariable(value = "url") String slug) {
_logger.debug("Editing article " + slug);
Article article = contentService.getGlobalArticle(slug, getLocale());
Article article = contentService.getGlobalArticle(slug, getLocale(), false);
if (article == null) {
article = new Article();
article.setSlug(slug);
......
......@@ -53,6 +53,7 @@ pagination.previous-page=< Previous
# Language
locale.language.change=Change locale
i18n.content-not-translated=The content is not available in your language. Please contact us if you can help!
# Languages (Do not translate)
locale.language.en=English
......
......@@ -13,7 +13,11 @@
</h1>
</c:if>
<div class="">
<c:if test="${article.lang != pageContext.response.locale.language}">
<%@include file="/WEB-INF/jsp/not-translated.jsp" %>
</c:if>
<div class="article" dir="${article.lang=='fa' || article.lang=='ar' ? 'rtl' : 'ltr'}">
<security:authorize access="hasRole('ADMINISTRATOR')">
<a href="<c:url value="/content/${article.slug}/edit" />" class="close">
<spring:message code="edit" />
......
<%@include file="/WEB-INF/jsp/init.jsp"%>
<div class="free-text blurp">
<div class="free-text blurp" dir="${blurp.lang=='fa' || blurp.lang=='ar' ? 'rtl' : 'ltr'}">
<c:out value="${blurp.body}" escapeXml="false" />
</div>
<c:if test="${blurp!=null && blurp.lang != pageContext.response.locale.language}">
<%@include file="/WEB-INF/jsp/not-translated.jsp" %>
</c:if>
<%@include file="/WEB-INF/jsp/init.jsp"%>
<div class="alert alert-warning translationmissing">
<spring:message code="i18n.content-not-translated" />
</div>
......@@ -515,7 +515,7 @@ tr.acn .sel.picked {
vertical-align: top;
}
.form-behind .editor {
/* .form-behind .editor {
display: none;
}
......@@ -525,4 +525,8 @@ tr.acn .sel.picked {
.form-behind.editing .display {
display: none;
}
*/
.translationmissing {
padding: 0.3em;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment