How to localize database table
what's the best way to localize a database table, holding for example a Country?
1 Flat Table
CountryID|Code|NameEN|NameIT
----------------------------
1 |IT |Italy |Italia
SELECT CountryID, Code, NameEN AS Name
FROM Countries
2 Joined Tables
CountryID|Code
--------------
1 |IT
CountryID|LocalizationCode|Name
---------------------------------
1 |EN |Italy
---------------------------------
2 |IT |Italia
SELECT CountryID, C开发者_运维知识库ode, Name
FROM Countries INNER JOIN CountriesLoc ON Countries.CountryID = CountriesLocs.CountryID
WHERE LocalizationCode = 'EN'
Thank you!
I recommend going with the second option, although you appear to have some data-typos.
Country:
Id Code
===============
1 IT
Localized_Country:
CountryId LanguageCode LocalizedName
=========================================
1 IT Italia
1 EN Italy
Which you then query like so:
SELECT a.Id, b.LocalizedName
FROM Country as a
JOIN Localized_Country as b
ON b.CountryId = a.Id
AND b.LanguageCode = :InputLanguageCode
WHERE a.Code = :InputInternationalCountryCode
Wrap that (or something similar) up in a view, and you're golden.
Some recommendations:
You may want to push Language
(or some other type of Locale
concept) into it's own table. The key can either be an auto-increment value, or the international characters, doesn't much matter which.
Make sure to put a unique constraint on (CountryId, LanguageCode)
, just in case. And never forget your foreign keys.
I will go with second one, it's more flexible, just remember to create index on CountryId column in the second table.
I've just noticed your query:
SELECT CountryID, Code, Name
FROM Countries INNER JOIN CountriesLoc ON Countries.CountryID = CountriesLocs.CountryID
WHERE LocalizationCode = 'EN'
why are you adding
where LocalizationCode = 'EN'
Do you assume that for country Italy can be more than one language?
EDITED
SELECT CountryID, Code, Name
FROM Countries INNER JOIN CountriesLoc ON Countries.CountryID = CountriesLocs.CountryID
WHERE Code = 'EN'
精彩评论