Skip to content

Commit

Permalink
fixing messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Oct 2, 2024
1 parent f8fe425 commit 9971433
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String getLabel(final String bundle, final String label) {
public String getLabel(final String bundle, final String label, final List<Object> data) {
try {
var resourceBundle = fromClassLoader(bundle, siteProperties.locale());
if (resourceBundle != null) {
if (resourceBundle != null && resourceBundle.containsKey(label)) {
var messageFormat = new MessageFormat(resourceBundle.getString(label), siteProperties.locale());
return messageFormat.format(data.toArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ThemeMessageSource(SiteProperties siteProperties, Path messageFolder, Mes
public String getLabel (final String bundle, final String label) {

var message = priorizedMessageSource.getLabel(bundle, label);
if (!("[" + label + "]").equals(label)) {
if (!("[" + label + "]").equals(message)) {
return message;
}

Expand All @@ -59,16 +59,16 @@ public String getLabel (final String bundle, final String label) {
@Override
public String getLabel (final String bundle, final String label, final List<Object> data) {

var message = priorizedMessageSource.getLabel(bundle, bundle, data);
if (!("[" + label + "]").equals(label)) {
var message = priorizedMessageSource.getLabel(bundle, label, data);
if (!("[" + label + "]").equals(message)) {
return message;
}

try {
var resourceBundle = fromClassLoader(bundle, siteProperties.locale());
if (resourceBundle != null) {
if (resourceBundle != null && resourceBundle.containsKey(label)) {
var messageFormat = new MessageFormat(resourceBundle.getString(label), siteProperties.locale());
return messageFormat.format(data);
return messageFormat.format(data.toArray());
}
} catch (Exception e) {
log.error("bundle not found", bundle);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.condation.cms.core.messages;

/*-
* #%L
* cms-core
* %%
* Copyright (C) 2023 - 2024 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/


import com.condation.cms.api.SiteProperties;
import java.nio.file.Path;
import java.util.Map;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
*
* @author t.marx
*/
public class ThemeMessageSourceTest {

private static DefaultMessageSource messageSource;
private static ThemeMessageSource themeMessageSource;

@BeforeAll
public static void setup() {
final SiteProperties siteProperties = new SiteProperties(Map.of("language", "de"));
messageSource = new DefaultMessageSource(
siteProperties,
Path.of("src/test/resources/messages")
);

themeMessageSource = new ThemeMessageSource(
siteProperties,
Path.of("src/test/resources/parent_messages"),
messageSource
);
}

@Test
public void from_child() {
var label = themeMessageSource.getLabel("abundle", "message.child");
Assertions.assertThat(label).isEqualTo("hello child");
}

@Test
public void from_parent() {
var label = themeMessageSource.getLabel("abundle", "message.parent");
Assertions.assertThat(label).isEqualTo("hello parent");
}

@Test
public void override_parent() {
var label = themeMessageSource.getLabel("abundle", "parent");
Assertions.assertThat(label).isEqualTo("I override the parent");
}
}
2 changes: 2 additions & 0 deletions cms-core/src/test/resources/messages/abundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
###

button.submit=Absenden
message.child=hello child
parent=I override the parent
23 changes: 23 additions & 0 deletions cms-core/src/test/resources/parent_messages/abundle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# #%L
# cms-api
# %%
# Copyright (C) 2023 - 2024 Marx-Software
# %%
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/gpl-3.0.html>.
# #L%
###

message.parent=hello parent
parent=I'm the parent
3 changes: 2 additions & 1 deletion cms-server/hosts/features/messages/labels.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
contact.form.title={0} Formular
contact.form.button.submit=Abschicken
contact.form.button.submit=Abschicken
message.site=Site
3 changes: 2 additions & 1 deletion cms-server/themes/parent/messages/labels.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
contact.form.button.submit=Submit
contact.form.button.submit=Submit
message.theme.parent=Parent theme
3 changes: 2 additions & 1 deletion cms-server/themes/test/messages/labels.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
contact.form.button.submit=Submit
contact.form.button.submit=Submit
message.theme.child=Child theme
11 changes: 10 additions & 1 deletion cms-server/themes/test/templates/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<nav th:replace="libs/fragments.html :: navigation"></nav>

<div id="content">
<h3>Overridden template from parent</h3>
<div class="conainter">
<h3>Overridden template from parent</h3>
</div>

<div class="container" th:utext="${content}"></div>

Expand All @@ -21,6 +23,13 @@ <h3>Overridden template from parent</h3>
</th:block>
</div>

<div class="conainter">
<h3>Messages</h3>
<h3 th:text="${messages.getLabel('labels', 'message.theme.child')}">message.theme.child</h3>
<h3 th:text="${messages.getLabel('labels', 'message.theme.parent')}">message.theme.parent</h3>
<h3 th:text="${messages.getLabel('labels', 'message.site')}">message.site</h3>
</div>

</div>

<th:block th:replace="libs/fragments.html :: footer"></th:block>
Expand Down

0 comments on commit 9971433

Please sign in to comment.