Using dictionary items in multi-site multi-lingual
Here is a snippet of code I'd like you guys to comment. Basically my issue is that I have a multi-site-multi-lingual setup (think 2 websites, FR / EN). Bothsites have a search box, so basically, I end up with the following dictionaries:
- SiteA - SearchButton - SiteB - SearchButton
Now I could simply prefix them SiteA_SearchButton. Even streamline that in my views. But here is the code I'm trying out, tell me what you think.
/// <summary>
/// Follows a trail of dictionary keys to find the one we want.
/// Syntax: key1.key2.key3
/// </summary>
/// <param name="keys"></param>
/// <param name="languageId"></param>
/// <returns></returns>
public static string GetDictionaryValue(string keys, int languageId) {
var keyArray = keys.Split('.');
// Starting point
var dictionaryItem = new umbraco.cms.businesslogic.Dictionary.DictionaryItem(keyArray[0]);
if (dictionaryItem == null)
{
// Can't find the starting point, return the last key of the array as a fallback behavior.
return keyArray.LastOrDefault();
}
foreach(var key in keyArray.Skip(1)) {
if (!dictionaryItem.hasChildren)
{
break;
}
dictionaryItem = dictionaryItem.Children.FirstOrDefault(x => x.key == key);
if (dictionaryItem == null)
{
// Since we still have keys to find, but can't find the path, return the last key of the array as a fallback behavior.
return keyArray.LastOrDefault();
}
}
// Return the corresponding value
return dictionaryItem.Value(languageId);
}
Using dictionary items in multi-site multi-lingual
Here is a snippet of code I'd like you guys to comment. Basically my issue is that I have a multi-site-multi-lingual setup (think 2 websites, FR / EN). Bothsites have a search box, so basically, I end up with the following dictionaries:
- SiteA
- SearchButton
- SiteB
- SearchButton
Now I could simply prefix them SiteA_SearchButton. Even streamline that in my views. But here is the code I'm trying out, tell me what you think.
Then in my view:
I think that languageId lookup could be better, but for the moment it's sufficient.
Basically the code allows be to navigate my dictionary items like this SiteA.SearchButton.
What do you guys think? Anything wrong here or to make better?
Thanks!
Gah!! I just realised, I can't have 2 dictionary items with the same keys. Phew, really not much to do with these, I guess it's back to prefix hell :(
is working on a reply...