Adding Site Culture using ResourcesConfig API. It is possibl

Posted by Community Admin on 04-Aug-2018 18:04

Adding Site Culture using ResourcesConfig API. It is possible?

All Replies

Posted by Community Admin on 31-Jan-2012 00:00


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.

Posted by Community Admin on 03-Feb-2012 00:00

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

Posted by Community Admin on 03-Feb-2012 00:00

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.

Posted by Community Admin on 27-May-2013 00:00

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

This thread is closed