Domanda

I developing web application in asp.net with multilanguage support. Let says that I have this datatable (I can not change it) ITEMS.

ID ITEM LANGUAGE

1 car en

1 das auto de

2 door en

3 desk en

3 tisch de

If user uses german language then sql looks like that:

SELECT id, item from ITEMS WHERE language = 'de'

I get that results:

1 das auto
3 tisch

Problem is, that all rows are not translated, so if I want to display all data where language field is 'de', there results are just items with langugage field 'de'. I want that if there is no 'de' field for some row, than it should display default language ('en').

In this case results should be

1 das auto
2 door (there is no translation for door, so english version should display)
3 tisch

I tried with ISNULL in sql statement, but with no success.

Just for information, i am using stored procedures.

Please, help me out.

È stato utile?

Soluzione

You can use Coalesce and joins. Note that in this case the ISNULL would do the same as COALESCE, but looks better for this application in case you add some new language.

SELECT i1.id, COALESCE(i2.item,i1.item) from Items i1 
LEFT JOIN Items i2 ON i2.id = i1.id AND i2.language = 'de' 
WHERE i1.language = 'en'

If you need that above more columns, but above the same tables, you can just update the COALESCE entry. For example the following query will show you the language as well:

SELECT i1.id, COALESCE(i2.item,i1.item) AS Item, COALESCE(i2.language, i1.language) Language from Items i1 
LEFT JOIN Items i2 ON i2.id = i1.id AND i2.language = 'de' 
WHERE i1.language = 'en'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top