+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Adding Site Culture using ResourcesConfig API. It is possible?

Adding Site Culture using ResourcesConfig API. It is possible?

4 posts, 0 answered
  1. Ronnie
    Ronnie avatar
    31 posts
    Registered:
    20 Jul 2011
    31 Jan 2012
    Link to this post

    I'm trying to register a second language in Custom Module Installation method.
    Basically how..?  a sample maybe...?

    public static void RegisterSiteCulture(CultureInfo info)
            {
                var configManager = ConfigManager.GetManager();
                ResourcesConfig resConfig = Config.Get<ResourcesConfig>();
                if (!resConfig.Cultures.ContainsKey(info.Name))
                {
                    var cultureElement = new CultureElement();
                    resConfig.Cultures.Add(cultureElement);
                    configManager.SaveSection(resConfig);
                }
            }

    I'm using this piece of code but CultureElement requires a parent Config element. ( What I supposed to send here).

    Searching in Sitefinity for "ResourcesConfig" or "CultureElement" doesn't return much information..

    Thanks.
  2. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    02 Dec 2016
    03 Feb 2012
    Link to this post
    Hi,

    That's a tough one, installing second culture only within the scope of a module is not plausible. This behavior is by design though - since the culture needs to be changed in the context of the application your module will be running, there's no way not to affect the application as well. However, once registering your module's resources, you are automatically able to localize them if you add another culture to your site from the Administration-> Interface labels and messages functionality of Sitefinity backend. Can you please let us know if there's a specific use case scenario behind this request, and if so maybe give us some additional details so we can try and advise you more specifically.

    Kind regards,
    Boyan Barnev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Ronnie
    Ronnie avatar
    31 posts
    Registered:
    20 Jul 2011
    03 Feb 2012
    Link to this post

    Thanks for your feedback,
    Actually yes it is difficult and not natural to Sitefinity.

    In standard ASP .Net we usually deliver more than one language (resources) with our solution... Now moving to Sitefinity we wanted to do the same, so our international clients get our product with the appropriated translations.

    In a separated thread Victor
    advised me not to register extra languages on custom module installation methods because it will load unnecessary code and data. Good one.. We agreed..!!


    Now we changed our strategy, want to provide language packages to manually load them as necessary using the Administration -> Interface Labels & Messages -> Import Language Pack (Excel).

    We are just waiting some feedback to understand if this is the best approach.

    Thanks.

  4. Bob
    Bob avatar
    25 posts
    Registered:
    14 Aug 2012
    27 May 2013
    Link to this post
    Hello,

    I found a way to include an extra language through the API. Like already mentioned, it's not recommended, but maybe someone is helped with the code.

    public void AddLangauges(string jsonFile)
    {
        // source: Configuration service (ConfigSectionItems.SaveLocalizationBasicSettings)
        ConfigManager manager = ConfigManager.GetManager();
        using (new ElevatedModeRegion(manager))
        {
            var section = manager.GetSection<ResourcesConfig>();
     
            if (section.Cultures.Count > 1)
            {
                // languages already installed
                return;
            }
     
            var json = ResourceHelper.ReadTextResourceFile(this.GetType(), jsonFile);
            var settings = JsonConvert.DeserializeObject<ItemContext<LocalizationSettingsModel>>(json);
            settings.Item.Apply(ref section);
            manager.SaveSection((ConfigSection)section);               
        }
        SystemManager.RestartApplication(true, true);
    }

    The Json file:
    {
        "Item" : {
            "BackendCultures" : [{
                    "Culture" : "en",
                    "DisplayName" : "English",
                    "FieldSuffix" : "",
                    "IsDefault" : true,
                    "Key" : "english-en",
                    "ShortName" : "en",
                    "SitesNames" : [],
                    "SitesUsingCultureAsDefault" : [],
                    "UICulture" : "en"
                }
            ],
            "Cultures" : [{
                    "Culture" : "en",
                    "DisplayName" : "English",
                    "FieldSuffix" : "",
                    "IsDefault" : true,
                    "Key" : "english-en",
                    "ShortName" : "en",
                    "SitesNames" : [],
                    "SitesUsingCultureAsDefault" : [],
                    "UICulture" : "en"
                }, {
                    "Culture" : "nl",
                    "DisplayName" : "Dutch",
                    "FieldSuffix" : "",
                    "IsDefault" : false,
                    "Key" : "dutch-nl",
                    "ShortName" : "nl",
                    "SitesNames" : [],
                    "SitesUsingCultureAsDefault" : [],
                    "UICulture" : "nl"
                }
            ],
            "DefaultLocalizationStrategy" : "SubFolderUrlLocalizationStrategy",
            "DefaultStrategySettings" : [],
            "MonolingualCulture" : null,
            "SubdomainStrategySettings" : [{
                    "DisplayName" : "English",
                    "IsDefault" : false,
                    "Key" : "english-en",
                    "Setting" : null
                }, {
                    "DisplayName" : "Dutch",
                    "IsDefault" : false,
                    "Key" : "dutch-nl",
                    "Setting" : null
                }
            ],
            "LastModified" : "\/Date(1368703331810)\/"
        }
    }

    The resource helper:
    public static class ResourceHelper
    {
        /// <summary>
        /// Reads a textual resource file.
        /// </summary>
        /// <param name="relativeFrom">Type to use to detect relative path.</param>
        /// <param name="resourcePath">Path to embedded resource, if resourcePath doesn't start with '/', it's relative to the namespace of the relativeFrom type.</param>
        public static string ReadTextResourceFile(Type relativeFrom, string resourcePath)
        {
            using (var stream = ReadResourceFile(relativeFrom, resourcePath))
            {
                using (var reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }
     
        /// <summary>
        /// Reads a resource file.
        /// </summary>
        /// <param name="relativeFrom">Type to use to detect relative path.</param>
        /// <param name="resourcePath">Path to embedded resource, if resourcePath doesn't start with '/', it's relative to the namespace of the relativeFrom type.</param>
        public static Stream ReadResourceFile(Type relativeFrom, string resourcePath)
        {
            var resPath = new StringBuilder(resourcePath);
            resPath.Replace('/', '.');
            resPath.Replace('\\', '.');
     
            // Is resource path relative?
            if (resPath[0] == '.')
                resPath.Remove(0, 1);
            else
                resPath.Insert(0, relativeFrom.Namespace + ".");
     
            // Get resource file.
            resourcePath = resPath.ToString();
     
            Stream stream = relativeFrom.Assembly.GetManifestResourceStream(resourcePath);
     
            if (stream == null)
            {
                throw new ArgumentException("Unable to find resource: " + resourcePath);
            }
     
            return stream;
        }
     
    }

    bye,

    bob
4 posts, 0 answered