I’m trying to implement i18n features in my grails app, but not only for messages (menus, labels, etc…). I’m trying to do it for the actual text fields of my domain classes.
So far, my solution consists of creating a domain class like this :
class LocalizedText {
String text
Locale locale
static hasMany=[translations : LocalizedText]
String getTranslation(String locale) {
def translation = this.translations.find(locale:locale)
if (translation != null) {
return translation
} else {
return this
}
}
}
Then, for example I would have this domain class with a single translatable field :
class News {
LocalizedText contents
// [...]
}
Then, on my controller, I check the lang parameter and pick up a translation if it exists, or the default language if no translation is available for the given locale.
e.g. :
class NewsController {
def list = {
def newsContents = []
News.list().each {
newsContents << [contents : it.contents.getTranslation(params.lang)]
}
[news : newsContents]
}
}
Sorry if there are errors but this is me coding ideas out of my head in a web form 🙂
I believe there are much cleaner solutions out there, so I just wanted you to get me started on this complex topic.
Thanks !
You should have a look at the “i18n Fields” plugin.