ORM Scheme/Plugin for Domain Class Translations
My problem simply put in Grails: I want a plugin OR example for translations of Domain Classes.
Description: I would like to have translatable entites with a nice DB scheme behind it. For example
Domain Class 1: Book (id, author_id, number_pages)
Translations for Book: (book_id, language, title, description)
Domain Class 2: Author (id, birthday)
Translations for Author (author_id, language, first_name, last_name) (for example, the same author is known under different names in different countries)
What I want to do: knowing the Language, I want to get The book with an ID, and EAGER fetching its author (of course book and author with the right translations). Or for example to search all books by an author, known by a first_name in a country/language.
How would you do manually that with GORM?
Is there a plugin for that or开发者_C百科 something to hide the "business logic"?
Or how could I simply get all translations for a book?
Whab about just using GORM as is - i.e. Book and BookTranslations would be two domain objects with a one to many translation between Book and BookTranslations, and then you could do BookTranslations.findAllByBook, criteria queries, etc.
The reason I suggest this is that there is an entity relationship in your description (i.e. one to many), so it's not like a parent/child/embedded style class heirarchy would do anything for you. This also gives you complete control over the fetching strategy[1]
I think the only 'secret sauce' here is that you could standardize on the way you represented languages. For example, if you actually had some unicode text in those rows or mappings to a standard messages files, then you could use the ISO standard language names for the language column in the BookTranslation table (e.g. en-US for US English). This would allow you to take direct advantage of the Grails internationalizatino features, where locales and message files can be automatically resolved, plus there are taglibs and other goodies to make internationalization less painful.
See [2]
A simple example would be something like the following (not tested, so pardon any compile errors):
class Book {
String author
int numPages
static hasMany [ bookTranslations: BookTranslations]
}
class BookTranslations {
String language
String title
String description
static belongsTo = Book
}
def book = new Book(author:"test", numPages:20)
book.save()
def translation1 = new BookTranslations(language:"en-us", title:"How to Book", description:"A wonderful book")
book.addToBookTranslations(translation1)
def translation2 = new BookTranslations(language:"en-us", title:"Second How to Book", description:"A glorious book")
book.addToBookTranslations(translation2)
def results = BookTranslations.findAllByBook(book)
// or ..
results = book.bookTranslations
As luck would have it, the example in the GORM chapter in the Grails Documentation is all about authors and books. See [3]
[1] http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html#5.5.2.8%20Eager%20and%20Lazy%20Fetching
[2] http://grails.org/doc/latest/guide/10.%20Internationalization.html
[3] http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html
Just stumbled upon this plugin, seems like it may be what you are looking for.
精彩评论