Adding Site Culture using ResourcesConfig API. It is possible?
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);
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
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
);
"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)\/"
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;