rodionmoiseev / c10n

A Java library, focused on making internationalisation more modular, easier to evolve and maintain, robust-to-change and IDE-friendly without excess of external tools.
Apache License 2.0
67 stars 10 forks source link

Question with messages #26

Closed kevinrobayna closed 9 years ago

kevinrobayna commented 9 years ago

Hi there, i just saw your library and i had to try it because i think is a great idea use interface as elements to translate.

Trying a example code like:

import c10n.C10N;
import c10n.annotations.DefaultC10NAnnotations;
import c10n.annotations.En;
import c10n.annotations.Ru;

interface Message {
  @En ("Hi")
  @Ru ("Привет")
  String grettins ();
}
public class MainTest {
  private static final Message msg = C10N.get(Message.class);
  public static void main (String [] args) {
    C10N.configure(new DefaultC10NAnnotations());
    System.out.println(msg.grettins());
  }
}

This is my output

Message.grettins

But it should be Hi or Привет. You know which could be my problem?

In addition i would like to add spanish annotation language to c10n i just have to modify core/src/main/java/com/github/rodionmoiseev/c10n/annotations/ adding a class named es.java and DefaultC10NAnnotations adding

 bindAnnotation(En.class).toLocale(Locale.SPANISH);
rodionmoiseev commented 9 years ago

Hi, sorry for taking this long.

There is a problem related to initialization order. The line

private static final Message msg = C10N.get(Message.class);

gets executed before the code in main(...), and therefore creates a Message class that does not contain any configuration.

For c10n to work, you need to call C10N.configure(...) prior to any calls to C10N.get(...) method. Thus the code below will work (unless you have the other problem below):

public class MainTest {
  public static void main (String [] args) {
    C10N.configure(new DefaultC10NAnnotations());
    Message msg = C10N.get(Message.class);
    System.out.println(msg.grettins());
  }
}

In relation to you second question, the other possible problem is that may arise is your default Locale setting (you can check it out with Locale.getDefault()). If your default locale does not match any of the locales registered in DefaultC10NAnnotations, it will not work.

In that case you will either need to add the relevant locale setting, or setup fallback. See examples below on how to do that:

C10N.configure(new C10NConfigBase() {
  @Override
  protected void configure() {
    //install default annotations (optional)
    install(new DefaultC10NAnnotations());

    //install custom @Es annotation bound to "es_ES" locale
    bindAnnotation(Es.class).toLocale(new Locale("es"));

    // ... or just setup c10n to fallback to @En annotation when locale does 
    // not match any other registered annotation, by binding it without specifying
    // the locale
    bindAnnotation(En.class);
  }
});

Of course, you can do the set-up you have mentioned at the end of your post. However, it may cause difficulties when you update your application to use @En annotation for English, instead of Spanish.

kevinrobayna commented 9 years ago

Oh thank you. I'm going to try it.

rodionmoiseev commented 9 years ago

Related to #18