I'd like to use the C10N library to manage translations in a Java project, but I don't understand how to use it to enable classes generate translated strings.
Please consider the following single-language example.
public interface Pet {
public String getBreed();
}
public class Dog implements Pet {
private String breed;
public Dog(String breed) {
this.breed = breed;
}
public String getBreed() {
return this.breed;
}
}
Then, I'd like to change the previous implementation to support i18n.
So, I could declare some translations using C10N:
public interface DogBreeds{
@En("dalmatian")
@It("dalmata")
String dalmatian();
@En("poodle")
@It("barboncino")
String poodle();
}
Now, I'd like to use one of these methods in a class to return a translated String.
The idea is to pass somehow the method to the class and then call it from a class' method.
In other words, the class should be able to return the correct translation based on the current locale, that may change at runtime.
public class Dog implements Pet {
public Dog(??) {
// the class should receive all the translations
// related to a specific breed
}
public String getBreed() {
// here I want to return the correct breed name translation,
// based on the current locale
return ??;
}
}
The Dog
class should not access the DogBreeds
interface directly. For example, Dog
may be part of a library, whereas DogBreeds
may be declared in the main project.
The Dog
class shouldn't hold just one translation based on the current locale, but all the available translations, so that it can return the correct one if the locale changes.
Is this feasible is a clean way?
If not, what are the alternatives?