Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sébastien Richer 194 posts 430 karma points
    Nov 19, 2014 @ 16:37
    Sébastien Richer
    0

    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);
    
            }

    Then in my view: 

            <div id="search">
                <form method="get" action="@search.url">
                    @{
                        var languageId = umbraco.cms.businesslogic.web.Domain.GetDomainsById(lang.Id)[0].Language.id;
                    }
                    <input type="text" id="search-input" name="s" placeholder="@Utilities.GetDictionaryValue("SiteA.SearchButton", languageId)" />
                    <input type="submit" id="submit-search" value="" />
                </form>
            </div>

    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!

  • Sébastien Richer 194 posts 430 karma points
    Nov 19, 2014 @ 16:40
    Sébastien Richer
    0

    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 :(

Please Sign in or register to post replies

Write your reply to:

Draft