Extending NameValueListBase for multilanguage purposes

Extending NameValueListBase for multilanguage purposes

Old forum URL: forums.lhotka.net/forums/t/652.aspx


stefan posted on Tuesday, July 18, 2006

Hi,

in order to facilitate at least multilingual reports, I need to find a way to provide my presentation layer with the translated versions of the strings that are kept in all those lookup-tables.
Think of a 'payment mode' that's presented depending on the origin of the customer.

First I thought of extending NameValueListBase, providing it with a Lanuage_ID Property somewhere, i.e. inside its criteria class, making all NameValueListBase derived objects language-aware by default.

I wonder if  there's an easier way to accomplish this.
Perhaps I should just inherit from NameValueListBase, add the language property, and use this one as my base class in the future, thus avoiding conflicts with future CSLA updates.

Any help is appreciated.

Stefan





RockfordLhotka replied on Tuesday, July 18, 2006

I would suggest your second option: creating a subclass of NVLB to add the language property.

However, I would also suggest that .NET has a partial answer for you in the form of Culture. On the Thread object you'll find a couple culture properties, and most code keys off the UICulture for this sort of thing.

The data portal even carries the client-side culture values over to the server (look at Chapter 4 and the discussion of ApplicationContext), so you get consistent access to the user's culture even on the application server.

If you can use the built-in culture value, perhaps you don't need a custom NVLB at all, but rather can just have your DataPortal_Fetch() rely on this standard value.

stefan replied on Tuesday, July 18, 2006

RockfordLhotka:

...

However, I would also suggest that .NET has a partial answer for you in the form of Culture. On the Thread object you'll find a couple culture properties, and most code keys off the UICulture for this sort of thing.

...

If you can use the built-in culture value, perhaps you don't need a custom NVLB at all, but rather can just have your DataPortal_Fetch() rely on this standard value.



Thanks Rocky for your reply an your hint towards the Thread.Culture object and DataPortal features.

However they intend to provide what's called localization (Is this the right word?).
So it's something that's client-based. I need something that's data-based.
Remember the customer from India, who's receiving his invoice in English (data-based),
whereas our software-user edits the  invoice in his localized language (client-based).

So I will go with my second option.

Thanks again,

Stefan


RockfordLhotka replied on Tuesday, July 18, 2006

No problem, use what works Smile [:)]

But to be clear, the thread culture settings are for locationalization - that's why they are in .NET in the first place. The culture specifies the language in use on the client workstation, which is typically the language the user wants to see. .NET automatically uses this culture setting to pull resources based on that culture (CSLA .NET uses this to localize all its string values for instance).

However, you can get the language value (such as en for english, ru for russian, etc.) from the culture object as well. Your code can then use that language value to tailor its data queries.

Of course I could be totally misunderstanding your problem - but if all you need to do is pass a language value (such as en, es, ro, it and so forth) to the server so your data portal code can do the right queries per-language, then culture already does this for you.

On the other hand, if your database includes the customer's language choice and you are tailoring the data queries based on that customer data value, then I don't see why you'd pass it via criteria at all. The value is already in the database after all, so it doesn't (to me anyway) make sense to pull it back to the client just to send it to the server again.

kids_pro replied on Sunday, July 23, 2006

Rocky,

I also run into the same situation.
Using thread culture might not work since windows does not know my culture yet.
What I been thinking is to path something to dataportal so that it return correct language.

Thanks,
Kids

Bonio replied on Monday, July 24, 2006

With reference to:

"..I would suggest your second option: creating a subclass of NVLB to add the language property..."

....as a relative noob to programming, how would you go about subclassing the NVLB or more particularly its Criteria class?

Sorry for the dumbass question, but still learning Smile [:)]


Thanks

Bonio

RockfordLhotka replied on Monday, July 24, 2006

You can see how to do this in chapters 7 and 8.
 
Rocky

....as a relative noob to programming, how would you go about subclassing the NVLB or more particularly its Criteria class?

Gort replied on Thursday, November 04, 2010

I did it this way:
 
 //Factory Method
 private static Hashtable _list;
        public static AddressTypeList GetAll()
        {
            if (_list == null)
            { //initial load, just get the default culture list
                _list = new Hashtable();
                _list[Thread.CurrentThread.CurrentCulture.Name] = DataPortal.Fetch<AddressTypeList>();
            }
            else if (_list[Thread.CurrentThread.CurrentCulture.Name] == null)
            { //need to load the list for this culture
                _list[Thread.CurrentThread.CurrentCulture.Name] = DataPortal.Fetch<AddressTypeList>();
            }
            return (AddressTypeList)_list[Thread.CurrentThread.CurrentCulture.Name];
        }

And in my Fetch, I have:
 do
        {                                                               
            this.Add(new NameValuePair(reader.GetInt32("AddressTypeId"), ResourceHelper.GetResource(reader.GetString("AddressTypeCd"), ResourceFiles.Lookups, true)));
        } while (reader.Read());

In my database, I have 'AddressType_Home', 'AddressType_Work', etc. which matches key/values in my resource files.

Copyright (c) Marimer LLC