Multilingual fields in DB tables

前端 未结 5 425
北海茫月
北海茫月 2020-12-19 15:07

I have an application that needs to support a multilingual interface, five languages to be exact. For the main part of the interface the standard ResourceBundle approach can

相关标签:
5条回答
  • 2020-12-19 15:14

    The standard translation approach as used, for example, in gettext is to use a single string to describe the concept and make a call to a translate method which translates to the destination language.

    This way you only need to store in the database a single string (the canonical representation) and then make a call in your application to the translate method to get the translated string. No FKs and total flexibility at the cost of a little of runtime performance (and maybe a bit more of maintenance trouble, but with some thought there's no need to make maintenance a problem in this scenario).

    0 讨论(0)
  • 2020-12-19 15:14

    What about this: http://rob.purplerockscissors.com/2009/07/24/internationalizing-websites/ ...that is what user "Chochos" says in response #2

    0 讨论(0)
  • 2020-12-19 15:18

    The approach I've seen in an application with a similar problem is that we use a "text id" column to store a reference, and we have a single table with all the translations. This provides some flexibility also in reusing the same keys to reduce the amount of required translations, which is an expensive part of the project.

    It also provides a good separation between the data, and the translations which in my opinion is more of an UI thing.

    If it is the case that the strings you require are not that many after all, then you can just load them all in memory once and use some method to provide translations by checking a data structure in memory.

    With this approach, your beans won't have getters for each language, but you would use some other translator object:

     MyTranslator.translate(myBean.getNameTextId());
    
    0 讨论(0)
  • 2020-12-19 15:22

    Depending on your requirements, it may be best to have a separate label table for each table which needs to be multilingual. e.g.: you have a XYZ table with a xyz_id column, and a XYZ_Label table with a xyz_id, language_code, label, other_label, etc

    The advantage of this, over having a single huge labels table, is that you can do unique constraints on the XYZ_labels table (e.g.: The english name for XYZ must be unique), and you can do indexed lookups much more efficiently, since the index will only be covering a single table at a time (e.g.: if you need to look up XYZ entities by english name) .

    0 讨论(0)
  • 2020-12-19 15:38

    I had to do that once... multilingual text for some tables... I don't know if I found the best solution but what I did was have the table with the language-agnostic info and then a child table with all the multilingual fields. At least one record was required in the child table, for the default language; more languages could be added later.

    On Hibernate you can map the info from the child tables as a Map, and get the info for the language you want, implementing the fallback on your POJO like you said. You can have different getters for the multilingual fields, that internally call the fallback method to get the appropiate child object for the needed language and then just return the required field.

    This approach uses more table (one extra table for every table that needs multilingual info) but the performance is much better, as well as the maintenance I think...

    0 讨论(0)
提交回复
热议问题