tags:

views:

54

answers:

2

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 :-(

+1  A: 

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.

Pavel Minaev
Thanks, this makes sense. As to the other part of my problem: if I deleted Meal, ideally the translated strings should be removed as well. However a sime "static belongsTo" (probably) won't work if I use the StringKey/TranslatedString for other objects than meal...
Steve
A: 

Hi, I solved this by using the Grails Localizations plugin.

Stefan