用于域类翻译的 ORM 方案/插件

发布于 2024-11-16 07:41:19 字数 490 浏览 1 评论 0 原文

我的问题简单地放在 Grails 中:我想要一个用于翻译域类的插件或示例。

描述:我想要可翻译的实体,背后有一个很好的数据库方案。例如

域名类别 1:图书(id、author_id、number_pages)

图书翻译:(book_id、语言、标题、描述)

域名类别 2:作者(id、生日)

作者的翻译(author_id、语言、first_name、last_name)(例如,同一作者在不同的国家以不同的名字为人所知)

我想要做什么:了解语言,我想获得带有 ID 的书,并且 EAGER 获取其作者(当然是具有正确翻译的书和作者)。或者例如搜索某个国家/语言中的名字的作者的所有书籍。

您将如何使用 GORM 手动完成此操作?

是否有插件或隐藏“业务逻辑”的东西?

或者我怎样才能简单地获得一本书的所有翻译?

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 something to hide the "business logic"?

Or how could I simply get all translations for a book?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静 2024-11-23 07:41:19

就按原样使用 GORM 来说,即 Book 和 BookTranslations 将是两个域对象,在 Book 和 BookTranslations 之间进行一对多翻译,然后您可以执行 BookTranslations.findAllByBook、条件查询等。

我建议这样做的原因是,是您描述中的实体关系(即一对多),因此父/子/嵌入式样式类层次结构不会为您做任何事情。这也使您可以完全控制获取策略[1]

我认为这里唯一的“秘密武器”是您可以标准化表示语言的方式。例如,如果这些行中实际上有一些 unicode 文本或到标准消息文件的映射,那么您可以对 BookTranslation 表中的语言列使用 ISO 标准语言名称(例如 en-US 表示美国英语)。这将使您能够直接利用 Grails 国际化功能,其中可以自动解析区域设置和消息文件,此外还有标签库和其他好东西可以使国际化不那么痛苦。

请参阅 [2]

一个简单的示例如下所示(未经测试,因此请原谅任何编译错误):

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

幸运的是,Grails 文档中 GORM 章节中的示例都是关于作者和书籍的。请参阅 [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

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

旧街凉风 2024-11-23 07:41:19

刚刚偶然发现了这个 插件,看起来它可能就是您正在寻找的东西。

Just stumbled upon this plugin, seems like it may be what you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文