csteinhilber
Programmer
Okay... I'm trying to develop a localization system that will return localized strings for a web application.
Using MySQL 4.1 and ColdFusion.
My method accepts a string "key", and the desired language, then searches the localization table for the appropriate string in the specified language.
So the table is set up like:
I'm not guaranteed that a key has entries for all languages, nor am I guaranteed that the key exists for English, nor am I guaranteed that a key will exist at all for any language.
Soooooooo... here's what I want to have happen:
- If the key exists for a given language, return that value
- If the key does not exist for a given language, and the key exists for English, return the English value (default)
- If the key doesn't exist at all, return the key
The query I have so far is:
which works quite well, until one or the other subselects returns no rows (ie - no entry exists for that key in that language). Then, of course, the join can proceed and the whole thing returns no records.
I'm thinking I need some way to force the subselects to always return something, even if no rows actually match the WHERE (which is where the IFNULL would help me out... but doesn't). But I can't quite wrap my brain around how to do that.
Or maybe I'm looking at it completely wrong.
Anybody have any ideas?
Thanks in advance!
-Carl
Using MySQL 4.1 and ColdFusion.
My method accepts a string "key", and the desired language, then searches the localization table for the appropriate string in the specified language.
So the table is set up like:
Code:
|| id | key | language | localizedValue ||
===================================================
|| 1234 | Hello | en | Hello ||
|| 1235 | Hello | es | Hola ||
|| 1236 | Hello | de | Hallo ||
|| 1237 | Hello | fr | Bonjour ||
|| 1238 | Yes | es | Si ||
|| 1239 | Yes | fr | Oui ||
I'm not guaranteed that a key has entries for all languages, nor am I guaranteed that the key exists for English, nor am I guaranteed that a key will exist at all for any language.
Soooooooo... here's what I want to have happen:
- If the key exists for a given language, return that value
- If the key does not exist for a given language, and the key exists for English, return the English value (default)
- If the key doesn't exist at all, return the key
The query I have so far is:
Code:
SELECT
IFNULL(
localeValues.localizedValue,
IFNULL(
englishValues.localizedValue,
'Default (key) value'
)
) as returnValue
FROM
(
SELECT
localizedValue, key
FROM
localLanguageStrings
WHERE
language = 'en'
) AS englishValues
LEFT JOIN
(
SELECT
localizedValue, key
FROM
localLanguageStrings
WHERE
language = 'de'
) AS localeValues
ON localeValues.key = englishValues.key
WHERE englishValues.key = 'Hello'
which works quite well, until one or the other subselects returns no rows (ie - no entry exists for that key in that language). Then, of course, the join can proceed and the whole thing returns no records.
I'm thinking I need some way to force the subselects to always return something, even if no rows actually match the WHERE (which is where the IFNULL would help me out... but doesn't). But I can't quite wrap my brain around how to do that.
Or maybe I'm looking at it completely wrong.
Anybody have any ideas?
Thanks in advance!
-Carl