/// <summary> /// Assign the culture that is set as default content culture to the current site. /// </summary> private void HandleReAssigningCulture() { if (RequestHelper.IsPostBack()) { string arg = ValidationHelper.GetString(Control.Page.Request[Page.postEventArgumentID], String.Empty); if (arg.EqualsCSafe(ASSIGN_ARGUMENT_NAME)) { string culture = CultureHelper.GetDefaultCultureCode(siteInfo.SiteName); // Only default content culture is allowed to be assigned to the site in case there is no multilingual license CultureSiteInfoProvider.RemoveSiteCultures(siteInfo.SiteName); CultureSiteInfoProvider.AddCultureToSite(culture, siteInfo.SiteName); // Get info object of the default content culture to set value of the UniSelector CultureInfo ci = CultureInfoProvider.GetCultureInfoForCulture(culture); if (ci != null) { Control.Value = Convert.ToString(ci.CultureID); reloadData = true; } Control.ShowChangesSaved(); } } }
/// <summary> /// Handles OnAfterValidate event of the UI form. /// </summary> /// <param name="sender">Sender object</param> /// <param name="e">Event argument</param> protected void OnAfterValidate(object sender, EventArgs e) { int cultureId = Control.EditedObject.Generalized.ObjectID; string cultureCode = ValidationHelper.GetString(Control.GetFieldValue("CultureCode"), String.Empty).Trim(); string cultureAlias = ValidationHelper.GetString(Control.GetFieldValue("CultureAlias"), String.Empty).Trim(); // Check validity of culture code if (!CultureHelper.IsValidCultureInfoName(cultureCode)) { Control.ShowError(Control.GetString("Culture.ErrorNoGlobalCulture")); Control.StopProcessing = true; } // Neutral culture cannot be used if (CultureHelper.IsNeutralCulture(cultureCode) && !Control.StopProcessing) { Control.ShowError(Control.GetString("culture.neutralculturecannotbeused")); Control.StopProcessing = true; } // Check if culture already exists for new created cultures var cultureByCode = CultureInfoProvider.GetCultureInfoForCulture(cultureCode); if ((cultureByCode != null) && (cultureByCode.CultureID != cultureId) && !Control.StopProcessing) { Control.ShowError(Control.GetString("culture_new.cultureexists")); Control.StopProcessing = true; } // Check whether culture alias is unique if (!String.IsNullOrEmpty(cultureAlias) && !Control.StopProcessing) { CultureInfo cultureByAlias = CultureInfoProvider.GetCultureInfoForCulture(cultureAlias); if (((cultureByAlias != null) && (cultureByAlias.CultureID != cultureId)) || CMSString.Equals(cultureAlias, cultureCode, true)) { Control.ShowError(Control.GetString("Culture.AliasNotUnique")); Control.StopProcessing = true; } } // Show warning if culture is UI and there is no resx file bool isUiCulture = ValidationHelper.GetBoolean(Control.GetFieldValue("CultureIsUICulture"), false); if (!Control.StopProcessing && !LocalizationHelper.ResourceFileExistsForCulture(cultureCode) && isUiCulture) { string url = "https://devnet.kentico.com/download/localization-packs"; string downloadPage = String.Format(@"<a href=""{0}"" target=""_blank"" >{1}</a> ", url, HTMLHelper.HTMLEncode(url)); Control.ShowWarning(String.Format(Control.GetString("culture.downloadlocalization"), downloadPage)); } }
private static string GetCultureShortName(string cultureCode) { return($"{CultureInfoProvider.GetCultureInfoForCulture(cultureCode)?.CultureShortName ?? cultureCode}"); }
/// <summary> /// Handles btnOK's OnClick event - Save culture info. /// </summary> protected void btnOK_Click(object sender, EventArgs e) { // Validate the input string result = new Validator().NotEmpty(txtCultureName.Text.Trim(), rfvCultureName.ErrorMessage).NotEmpty(txtCultureCode.Text.Trim(), rfvCultureCode.ErrorMessage).Result; if (txtCultureCode.Text.Trim().Length > 10) { result = GetString("Culture.MaxLengthError"); } // Validate the culture code try { // Check if global culture exists if (new CultureInfo(txtCultureCode.Text.Trim()) == null) { result = GetString("Culture.ErrorNoGlobalCulture"); } } catch { result = GetString("Culture.ErrorNoGlobalCulture"); } txtCultureAlias.Text = URLHelper.GetSafeUrlPart(txtCultureAlias.Text.Trim(), String.Empty); string cultureAlias = txtCultureAlias.Text.Trim(); // Check whether culture alias is unique if (!String.IsNullOrEmpty(cultureAlias)) { CMS.SiteProvider.CultureInfo ci = CultureInfoProvider.GetCultureInfoForCulture(cultureAlias); if ((ci != null) || (CMSString.Compare(cultureAlias, txtCultureCode.Text.Trim(), true) == 0)) { result = GetString("Culture.AliasNotUnique"); } } if (result == "") { // Check if culture already exists CMS.SiteProvider.CultureInfo ci = CultureInfoProvider.GetCultureInfoForCulture(txtCultureCode.Text.Trim()); if (ci == null) { // Save culture info ci = new CMS.SiteProvider.CultureInfo(); ci.CultureName = txtCultureName.Text.Trim(); ci.CultureCode = txtCultureCode.Text.Trim(); ci.CultureShortName = txtCultureShortName.Text.Trim(); ci.CultureAlias = cultureAlias; CultureInfoProvider.SetCultureInfo(ci); URLHelper.Redirect("Culture_Edit_Frameset.aspx?cultureID=" + ci.CultureID + "&saved=1"); } else { ShowError(GetString("Culture_New.CultureExists")); } } else { ShowError(result); } }
/// <summary> /// Handles btnOK's OnClick event - Update resource info. /// </summary> protected void btnOK_Click(object sender, EventArgs e) { // finds whether required fields are not empty string result = new Validator() .NotEmpty(txtCultureName.Text.Trim(), rfvCultureName.ErrorMessage) .NotEmpty(txtCultureCode.Text.Trim(), rfvCultureCode.ErrorMessage) .Result; if (txtCultureCode.Text.Trim().Length > 10) { result = GetString("Culture.MaxLengthError"); } try { // Chech if global culture exists if (new System.Globalization.CultureInfo(txtCultureCode.Text.Trim()) == null) { result = GetString("Culture.ErrorNoGlobalCulture"); } } catch { result = GetString("Culture.ErrorNoGlobalCulture"); } txtCultureAlias.Text = URLHelper.GetSafeUrlPart(txtCultureAlias.Text.Trim(), String.Empty); string cultureAlias = txtCultureAlias.Text.Trim().Replace("'", "''"); // Check whether culture alias is unique if (!string.IsNullOrEmpty(cultureAlias)) { string where = string.Format("(CultureCode = N'{0} 'OR CultureAlias = N'{0}') AND CultureID <> {1}", cultureAlias, culture.CultureID); DataSet cultures = CultureInfoProvider.GetCultures(where, null, 1, "CultureID"); if ((!DataHelper.DataSourceIsEmpty(cultures)) || (CMSString.Equals(cultureAlias, txtCultureCode.Text.Trim(), true))) { result = GetString("Culture.AliasNotUnique"); } } if (result != string.Empty) { ShowError(result); return; } // finds if the culture code is unique CultureInfo uniqueCulture = CultureInfoProvider.GetCultureInfoForCulture(txtCultureCode.Text.Trim()); // if culture code already exists and it is just editing culture -> update if ((uniqueCulture == null) || (uniqueCulture.CultureID == culture.CultureID)) { UpdateCulture(); } // if culture code already exists and it is another culture -> error else { ShowError(GetString("Culture_New.CultureExists")); } }