Skip to content

Commit

Permalink
Implement i18nHelper mapped placeholder
Browse files Browse the repository at this point in the history
 - Document i18nHelper placeholder
  • Loading branch information
eschleb committed Mar 22, 2024
1 parent c1bd138 commit 47aa167
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
18 changes: 18 additions & 0 deletions Helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ Provides messages for a given key, translated using the locale of the content no
```handlebars
{{{t 'web.patterns.paging.next'}}}
```
#### placeholders
messages can contain either indexed or mapped placeholders.
##### indexed
```properties
web.patterns.paging.current=current page is {0}
```
```handlebars
{{{t 'web.patterns.paging.current' 2}}}
```
--> =current page is 2
##### map
```properties
web.patterns.paging.current=current page is {currentPagePlaceholder}
```
```handlebars
{{{t 'web.patterns.paging.current' currentPagePlaceholder=2}}}
```
--> =current page is 2

### [If condition](light-development/src/main/java/com/merkle/oss/magnolia/renderer/handlebars/helpers/IfConditionHelper.java)
Conditionally renders a block or returns evaluated condition.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.merkle.oss.magnolia.renderer.handlebars.helpers;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Options;
import com.merkle.oss.magnolia.renderer.handlebars.utils.LocaleProvider;
import info.magnolia.i18nsystem.FixedLocaleProvider;
import info.magnolia.i18nsystem.SimpleTranslator;
import info.magnolia.i18nsystem.TranslationService;
import info.magnolia.jcr.util.ContentMap;

import javax.inject.Inject;
import javax.jcr.Node;
import java.io.IOException;
import java.util.Locale;
import java.util.Set;

import javax.inject.Inject;
import javax.jcr.Node;

import org.apache.commons.text.StringSubstitutor;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Options;
import com.merkle.oss.magnolia.renderer.handlebars.utils.LocaleProvider;

public class I18nHelper implements NamedHelper<String> {
private final TranslationService translationService;
private final LocaleProvider localeProvider;
Expand All @@ -32,7 +35,7 @@ public CharSequence apply(final String key, final Options options) {
final Node node = ((ContentMap) options.get("content")).getJCRNode();
final Locale locale = localeProvider.getLocale(node);
final SimpleTranslator simpleTranslator = new SimpleTranslator(translationService, new FixedLocaleProvider(locale));
final String message = simpleTranslator.translate(key, options.params);
final String message = new StringSubstitutor(options.hash, "{", "}").replace(simpleTranslator.translate(key, options.params));
return new Handlebars.SafeString(message);
}

Expand Down

0 comments on commit 47aa167

Please sign in to comment.