Copied to clipboard

Flag this post as spam?

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


  • Jeremy Coulson 61 posts 143 karma points
    May 01, 2017 @ 14:05
    Jeremy Coulson
    0

    How can I add a language that is not already in Umbraco?

    Hello! We have a multilingual site. For our Swedish site, we use the Norwegian language. I have been given the task of using the no-SE language, but there is no such option under Settings / Languages. How can I add to this list?

    Screen shot of the select list as it is now.

    Thanks!

    Jeremy

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    May 01, 2017 @ 15:17
    Nicholas Westby
    2

    I had to do similar recently. Basically, you have to create a console application and run it once to create the culture on each machine (e.g., staging, production):

    // Namespaces.
    using System;
    using System.Globalization;
    using System.Linq;
    
    /// <summary>
    /// Console application that ensures the necessary cultures exist on the system.
    /// </summary>
    /// <remarks>
    /// This console application was necessary because some systems don't contain the necessary
    /// cultures, and this logic couldn't reside within the website as the creation of new
    /// cultures requires administrative privileges.
    /// </remarks>
    class Program
    {
    
        #region Main
    
        /// <summary>
        /// Main entry point for the console application.
        /// </summary>
        static void Main(string[] args)
        {
            EnsureCulturesExist();
        }
    
        #endregion
    
        #region Private Methods
    
        /// <summary>
        /// Ensures all the necessary cultures exist (e.g., "en-AT" doesn't exist on some computers).
        /// </summary>
        /// <remarks>
        /// Some of this code comes from https://msdn.microsoft.com/en-us/library/system.globalization.cultureandregioninfobuilder.aspx
        /// and http://stackoverflow.com/a/16476935/2052963.
        /// </remarks>
        private static void EnsureCulturesExist()
        {
    
            // Variables.
            var allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
            var ignoreCase = StringComparison.InvariantCultureIgnoreCase;
    
            // Reusable cultures/regions.
            var english = CultureInfo.CreateSpecificCulture("en-US");
            var austria = new RegionInfo("AT");
            var germany = new RegionInfo("DE");
            var sweden = new RegionInfo("SE");
            var switzerland = new RegionInfo("CH");
    
            // Register English (Austria) / "en-AT".
            var englishAustriaExists = allCultures.Any(x => "en-AT".Equals(x.Name, ignoreCase));
            if (!englishAustriaExists)
            {
                var englishAustria = new CultureAndRegionInfoBuilder("en-AT", CultureAndRegionModifiers.None);
                englishAustria.LoadDataFromCultureInfo(english);
                englishAustria.LoadDataFromRegionInfo(austria);
                englishAustria.CultureEnglishName = "English (Austria)";
                englishAustria.CultureNativeName = "English (Austria)";
                TryRegisterCulture(englishAustria);
            }
    
            // Register English (Germany) / "en-DE".
            var englishGermanyExists = allCultures.Any(x => "en-DE".Equals(x.Name, ignoreCase));
            if (!englishGermanyExists)
            {
                var englishGermany = new CultureAndRegionInfoBuilder("en-DE", CultureAndRegionModifiers.None);
                englishGermany.LoadDataFromCultureInfo(english);
                englishGermany.LoadDataFromRegionInfo(germany);
                englishGermany.CultureEnglishName = "English (Germany)";
                englishGermany.CultureNativeName = "English (Germany)";
                TryRegisterCulture(englishGermany);
            }
    
            // Register English (Sweden) / "en-SE".
            var englishSwedenExists = allCultures.Any(x => "en-SE".Equals(x.Name, ignoreCase));
            if (!englishSwedenExists)
            {
                var englishSweden = new CultureAndRegionInfoBuilder("en-SE", CultureAndRegionModifiers.None);
                englishSweden.LoadDataFromCultureInfo(english);
                englishSweden.LoadDataFromRegionInfo(sweden);
                englishSweden.CultureEnglishName = "English (Sweden)";
                englishSweden.CultureNativeName = "English (Sweden)";
                TryRegisterCulture(englishSweden);
            }
    
            // Register English (Switzerland) / "en-CH".
            var englishSwitzerlandExists = allCultures.Any(x => "en-CH".Equals(x.Name, ignoreCase));
            if (!englishSwitzerlandExists)
            {
                var englishSwitzerland = new CultureAndRegionInfoBuilder("en-CH", CultureAndRegionModifiers.None);
                englishSwitzerland.LoadDataFromCultureInfo(english);
                englishSwitzerland.LoadDataFromRegionInfo(switzerland);
                englishSwitzerland.CultureEnglishName = "English (Switzerland)";
                englishSwitzerland.CultureNativeName = "English (Switzerland)";
                TryRegisterCulture(englishSwitzerland);
            }
    
        }
    
        /// <summary>
        /// Attempts to register the specified culture, swallowing any errors that may occur.
        /// </summary>
        /// <param name="culture">
        /// The culture to register.
        /// </param>
        private static void TryRegisterCulture(CultureAndRegionInfoBuilder culture)
        {
            try
            {
                culture.Register();
            }
            catch
            {
            }
        }
    
        #endregion
    
    }
    

    That basically copies existing cultures to create new ones. You can modify it slightly for the culture(s) you want to create.

  • Jeremy Coulson 61 posts 143 karma points
    May 11, 2017 @ 17:32
    Jeremy Coulson
    0

    Hey, I forgot to respond. This looks awesome. I was actually not able to do this because of certain restrictions in our environment, so I wrote some cheesy code to handle "if culture X is required, act like it's culture Y with these special changes."

    But good call for the insight. I see now that Umbraco just uses cultures in the system already and not some list specific to Umbraco. Thanks for the knowledge!

    Jeremy

  • Sigurd Aabøe-Sagen 12 posts 122 karma points
    May 11, 2017 @ 18:43
    Sigurd Aabøe-Sagen
    0

    Are you aware of that Norway and Sweden are two different countries? And they have to different languages? se-SE and nb-NO.

    Using Norwegian language in a Swedish site may give you letters not used in the Swedish language. And vice-versa.

    I guess you should use se-SE or just SE for your Swedish site. For your Norwegian site you should use nb-NO (Bokmål) since Nynorsk is a dialect.

    // Sigurd from Norway.

  • Jeremy Coulson 61 posts 143 karma points
    May 11, 2017 @ 22:52
    Jeremy Coulson
    1

    As a heavy metal listener, I am aware that Norway and Sweden are different countries. :) I actually don't know why my employer uses the same language code for both countries. That decision predates my involvement in the company.

  • Bjarne Fyrstenborg 1285 posts 4039 karma points MVP 8x c-trib
    May 11, 2017 @ 23:24
    Bjarne Fyrstenborg
    1

    It seems there exists a culture code se-NO .. and both se-SE and sv-SE.

    From what I have read the cultureInfo and country codes are OS dependant. http://azuliadesigns.com/list-net-culture-country-codes/

    https://msdn.microsoft.com/en-us/library/cc233982.aspx

    The culture in Umbraco backoffice are added to the dropdown here: https://github.com/umbraco/Umbraco-CMS/blob/5397f2c53acbdeb0805e1fe39fda938f571d295a/src/Umbraco.Web/umbraco.presentation/umbraco/create/language.ascx.cs#L23-L35

    Maybe this explains it further http://stackoverflow.com/a/22808123

    CultureInfo.GetCultures is not designed to be a complete and definitive list of all the cultures in the world. It's only designed to get you the cultures that can be found on the computer.

    /Bjarne

  • Jeremy Coulson 61 posts 143 karma points
    May 16, 2017 @ 14:37
    Jeremy Coulson
    0

    This is what I learned also. I thought there was a config file sitting around somewhere with a bunch of country and language elements in it, but it looks like Umbraco just knows about the cultures already on the server.

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft