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

Loading locale-sensitive external resources #2

Closed
rodionmoiseev opened this issue May 2, 2012 · 5 comments
Closed

Loading locale-sensitive external resources #2

rodionmoiseev opened this issue May 2, 2012 · 5 comments
Assignees
Milestone

Comments

@rodionmoiseev
Copy link
Owner

Inspired by https://developers.google.com/web-toolkit/doc/latest/DevGuideClientBundle#I18N

API draft:

@C10NMessages
public interface MyMessages{
  //locale mapped external file resources (or http, etc) 
  @En(extRes="file:///absolute/path/text.txt")
  @Ja(extRes="file://../relative/path/text.txt")
  String textFileContents();

  //Defer to ResourceBundle-like locale resolution 
  // (e.g. look for text_ja_JP.txt for Jp locale)
  @C10NResource(extRes="file://absolute/path/text.txt")
  String textFileContents2();

  //Access internal (in-the-jar) resources
  @En(intRes="com/example/text.txt")
  @C10NResource(intRes="com/example/text.txt")
  String internalResourceContents();
}
@ghost ghost assigned rodionmoiseev May 2, 2012
@eclectice
Copy link

I support your idea! Also, is it possible to allow such definitions like en_US, en_UK, etc. taking into account both the language and the country rather than just the language only?

@rodionmoiseev
Copy link
Owner Author

Thanks! Sure, you would be able to do that, because annotations can be bound to any Locale object, which you may define as new Locale("en", "US") or even new Locale("en", "US", "someDialect").

See how the default annotations are bound for reference: https://github.com/rodionmoiseev/c10n/blob/master/core/src/main/java/c10n/annotations/DefaultC10NAnnotations.java#L43

@eclectice
Copy link

I've already knew about Locale(). Sorry that my suggestion is not so direct: can it be done for annotations like @en_US, @en_UK, etc.?

@rodionmoiseev
Copy link
Owner Author

Sure! Just declare your own annotation:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface en_US {
  String value();
}

and bind it to the right locale, somewhere in your configuration:

 bindAnnotation(en_US.class).toLocale(new Locale("en", "US"));

rodionmoiseev pushed a commit that referenced this issue Jun 9, 2012
* Added support for retrieving content via absolute URL (file:// and http:// protocols tested)
* Added test dependency on apache commons io ver: 2.3
rodionmoiseev pushed a commit that referenced this issue Jun 9, 2012
* Moved C10N.UDEF to (new) Constants.UNDEF to remove package cyclic dependency
* Added `extRes` method to all built-in annotations
rodionmoiseev pushed a commit that referenced this issue Jun 9, 2012
* Added support for loading resource from the classpath (or inside the jar).
  Sample usage: @en(intRes="com/example/resources/data.txt")
* Added `intRes` method to all built-in annotations
@rodionmoiseev
Copy link
Owner Author

Got the most features to work, except the @C10NResource (automated suffixing with _en, _ja, etc.). This will be tricky to implement with the current architecture due to the fact that all translations have to be loaded and saved at proxy initialisation time (i.e. C10N.get(MyInterface.class)-time), but we only have the information about the current locale at this time, and cannot preload translations for other locales. This approach itself needs to be revised in order to allow to implement this feature completely. Therefore, I am closing this issue for now and will reopen it as another request if there is enough demand for it.

Edit: The internal resource path should look like intRes="com/example/resources/file.txt" (as opposed to com.example.resources.file.txt. I have updated the original issue post to reflect this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants