Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve i18n/locale support #534

Closed
a8cjones opened this issue Aug 26, 2016 · 12 comments
Closed

Improve i18n/locale support #534

a8cjones opened this issue Aug 26, 2016 · 12 comments
Labels
Milestone

Comments

@a8cjones
Copy link

Unless I'm missing something, there doesn't seem to be a easy way of supporting multiple locales within the same template, which results in needing multiple templates.

Using Locale.setDefault('xx') in Java is not thread-safe, so not a solution.

What I'd like ideally would be to have an alternative method signature to template.apply() which accepted a Locale object.
e.g. public String apply(Object context, Locale locale)

@jknack
Copy link
Owner

jknack commented Aug 26, 2016

I built a multi-lang app with i18n helper. Just one template multiples messages*.properties.

Not sure why we need a Locale in the apply method.

@a8cjones
Copy link
Author

a8cjones commented Aug 26, 2016

An example is this:-

I have a loop which is sending out mails. The language of the mail depends upon the language of the recipient.

I do have multiple messages*.properties, but how does that help in this case?

I can use Locale.setDefault("xx") prior to template.apply(), but it's not thread-safe, so sometimes the mail goes out in the wrong language (Locale is changed on another thread)

So, I have to "hard code" the locale into the template {{i18n "hello" locale="fr" bundle="messages"}} resulting in multiple templates.

@jknack
Copy link
Owner

jknack commented Aug 26, 2016

are you using i18n helper right?

Suppose there is a Recipient class with a lang attribute or getter which has the current locale/lang. Then:

Template template = ...;

for (Recipient r : recipients) {
  template.apply(r);
}
{{i18 "hi" locale=r.lang}},

{{i18 "body" locale=r.lang}},

Does it work?

@a8cjones
Copy link
Author

Ah, OK, that's what I'm missing. Let me give that a shot and see if it works (it should I think)

@a8cjones
Copy link
Author

I can't get your suggestion to work unfortunately, the lang from the Recipient (as per your example) just gets ignored, and I always get the default bundle. I can only get it to work if I hard-code the locale e.g. locale="fr", and then it works.

@jknack
Copy link
Owner

jknack commented Aug 26, 2016

that isn't possible! what expression did you for locale? My guess is that is resolving to null and that is the reason why it fallbacks to default.

In my example lang is a getLang method on Recipient

@a8cjones
Copy link
Author

Maybe I'm completely missing something, but here are my files...

i18n/mailbundle_fr.properties

hello=bonjour

i18n/mailbundle.properties

hello=hello

res/mailtemplate2.hbs

{{{i18n "hello" bundle="i18n/mailbundle" locale=r.lang}}} test

Recipient.java

public class Recipient {
public final String lang;
public Recipient(String lang) {
this.lang=lang;
}
public String getLang() {
return lang;
}
}

HandlebarsTest.java

public static void main(String[] args) throws IOException {
    Handlebars handlebars = new Handlebars();
    Template template = handlebars.compile("res/mailtemplate2");

    Recipient r = new Recipient("fr");
    System.out.println(template.apply(r));
}

output

hello test

@jknack
Copy link
Owner

jknack commented Aug 26, 2016

Yea it should work... will try it later today and get back to you

@a8cjones
Copy link
Author

Thanks, I appreciate it!

@jknack
Copy link
Owner

jknack commented Aug 26, 2016

oh wait! I see the problem, there is no r in the handlebars context:

template.apply(r);

you have a to use this, like:

{{{i18n "hello" bundle="i18n/mailbundle" locale=this.lang}}} test or just

{{{i18n "hello" bundle="i18n/mailbundle" locale=lang}}} test

👍

@a8cjones
Copy link
Author

That did the trick! Thanks!

@jknack
Copy link
Owner

jknack commented Aug 26, 2016

cool, glad it works!

Also, I invite you to try: http://jooby.org

@jknack jknack added this to the 4.0.7 milestone May 27, 2018
@jknack jknack closed this as completed May 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants