I need to store different versions of not very long strings for different languages (2-4 languages) in a Postgres table.
What is the best way of doing that? Array or JSON or something like that?
I need to store different versions of not very long strings for different languages (2-4 languages) in a Postgres table.
What is the best way of doing that? Array or JSON or something like that?
First make sure that the database locale can deal with different languages. Use an UTF-8 server-encoding. Optionally set
LC_COLLATE = 'C'
to be on neutral ground or use a collation for your first language to have a default sort order. Start by reading the chapter Collation Support in the manual.I would strongly suggest that you use the latest version of PostgreSQL (9.1 at time of writing) because it has superior collation support.
As for the table structure: keep it simple. It sounds like there is a low, fixed number of languages to deal with. You could just have a column for each language then:
This is pretty effective, even with many languages. NULL storage is very cheap.
If you have a varying number of languages to deal with, a separate table might be the better solution. This solution assumes that you have a "master language", where the string is always present:
Or, if a (two-letter) abbreviation is enough, just create a
enum
type to identify the language.Not treating the master language special and keeping all language variants in the same table might make handling in your app simpler. But it really depends on your requirements.