开发者

Localized database strings

I have a small Grails application that has the following domain:

class Meal {
  String name
  String description
  String allergyNote
}

For localization purposes the three strings should now be available in multiple languages. For example, while an English user would see name="Steak", a Spanish user should see name="Filete" in the output. I was thinking of doing the following:

class Language {
  String isoCode
  String languageName
}

class TranslatedString {
  Language language
  String   translation
}

but I am not sure how to link the Meals with the TranslatedStrings as it is开发者_开发技巧 used for three members, also I would like to use it for other classes (not just Meal) as well (or do I need to have separated tables, i.e. a MealNameTranslated, MealDescriptionTranslated, etc tables?). I know this is probably a stupid question, but I am a beginner and have not been able to figure this out :-(


Your TranslatedString class isn't complete, since there's no way to know what it is a translation of. You need to have one more entity here that provides some kind of identifier for the string:

// object/record identity is used as key
class StringKey {
  String keyName // purely descriptive, not actually used at runtime
}

class TranslatedString {
  // the following 2 form a primary key
  StringKey key
  Language  language

  String    translation
}

class Meal {
  StringKey name
  StringKey description
  StringKey allergyNote
}

Then you can look up translation given key and language.


You should also check out the article on Graeme Rocher's blog (He's head of Grails Development at SpringSource) The article explains how to move the standard static Grails i18n translation property files into the database. It also includes caching support.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜