/// <summary> /// Gets and updates resource string. Called when the "Get and update resource string" button is pressed. /// Expects the CreateResourceString method to be run first. /// </summary> private bool GetAndUpdateResourceString() { // Get the resource string ResourceStringInfo updateString = SqlResourceManager.GetResourceStringInfo("Test.MyNewResourceString"); if (updateString != null) { // Update the properties updateString.StringKey = updateString.StringKey.ToLower(); // Save the changes SqlResourceManager.SetResourceStringInfo(updateString); return(true); } return(false); }
/// <summary> /// Handles the UniGrid's OnAction event. /// </summary> /// <param name="actionName">Name of item (button) that threw event</param> /// <param name="actionArgument">ID (value of Primary key) of corresponding data row</param> protected void UniGridUICultures_OnAction(string actionName, object actionArgument) { ResourceStringInfo ri = SqlResourceManager.GetResourceStringInfo(actionArgument.ToString(), ui.UICultureCode); switch (actionName) { case "edit": URLHelper.Redirect(string.Format("Edit.aspx?stringid={0}&uicultureid={1}", ri.StringId, ui.UICultureID)); break; case "delete": SqlResourceManager.DeleteResourceStringInfo(actionArgument.ToString(), ui.UICultureCode); break; case "deleteResource": SqlResourceManager.DeleteResourceStringInfo(actionArgument.ToString(), CultureHelper.DefaultUICulture); break; } }
/// <summary> /// Create the string /// </summary> protected void btnCreate_Click(object sender, EventArgs e) { string key = QueryHelper.GetString("stringKey", "").Trim().Replace("'", "''"); // Create the key ResourceStringInfo rsi = SqlResourceManager.GetResourceStringInfo(key); if (rsi == null) { // Create the key rsi = new ResourceStringInfo(); rsi.StringKey = key; rsi.UICultureCode = CultureHelper.DefaultUICulture; rsi.TranslationText = key; SqlResourceManager.SetResourceStringInfo(rsi); URLHelper.Redirect(URLHelper.CurrentURL); } }
/// <summary> /// Handles the UniGrid's OnExternalDataBound event. /// </summary> protected object UniGridStrings_OnExternalDataBound(object sender, string sourceName, object parameter) { switch (sourceName.ToLowerCSafe()) { case "edititem": ImageButton ib = sender as ImageButton; if (ib != null) { GridViewRow gvr = parameter as GridViewRow; if (gvr != null) { DataView dv = gvr.DataItem as DataView; if (dv != null) { if (ui != null) { ResourceStringInfo ri = SqlResourceManager.GetResourceStringInfo(ValidationHelper.GetString(dv[0], ""), ui.UICultureCode); if (ri != null) { ib.OnClientClick = String.Format("location.href='Edit.aspx?stringid={0}&uicultureid={1}'; return false;", ri.StringId, ui.UICultureID); } } } } } break; case "stringiscustom": return(UniGridFunctions.ColoredSpanYesNo(parameter)); case "culturetext": case "defaulttext": return(MacroResolver.RemoveSecurityParameters(parameter.ToString(), true, null)); } return(parameter); }
/// <summary> /// Button localize click. In AutomaticMode available only. /// </summary> void btnLocalize_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(textbox.Text.Trim())) { // Get maximum length of resource key int maxKeyLength = MAX_KEY_LENGTH; string prefix = ResourceKeyPrefix; if (SettingsKeyProvider.DevelopmentMode && String.IsNullOrEmpty(ResourceKeyPrefix)) { prefix = PREFIX; } if (!String.IsNullOrEmpty(prefix)) { maxKeyLength -= prefix.Length; } // Initialize resource string string newResource = TextHelper.LimitLength(ValidationHelper.GetCodeName(textbox.Text.Trim()), maxKeyLength, String.Empty, true); int i = 0; if (!newResource.StartsWithCSafe(prefix)) { hdnValue.Value = prefix + newResource; } else { hdnValue.Value = newResource; } // If key exists then create new one with number as a suffix while (SqlResourceManager.GetResourceStringInfo(hdnValue.Value) != null) { // If newly created resource key is longer then allowed length then trim end by one character if ((prefix.Length + newResource.Length + ++i) > MAX_KEY_LENGTH) { newResource = newResource.Substring(0, newResource.Length - 1); } if (!newResource.StartsWithCSafe(prefix)) { hdnValue.Value = prefix + newResource + i; } else { hdnValue.Value = newResource + i; } } // Check if current user's culture exists UICultureInfo uiCulture = null; string cultureCode = CultureHelper.PreferredUICulture; try { uiCulture = UICultureInfoProvider.GetUICultureInfo(CultureHelper.PreferredUICulture); } // Use default UI culture catch { cultureCode = CultureHelper.DefaultUICulture; } // Use default UI culture if (uiCulture == null) { cultureCode = CultureHelper.DefaultUICulture; } // Save ResourceString ResourceStringInfo ri = new ResourceStringInfo(); ri.StringKey = hdnValue.Value; ri.UICultureCode = cultureCode; ri.TranslationText = textbox.Text; ri.StringIsCustom = !SettingsKeyProvider.DevelopmentMode; SqlResourceManager.SetResourceStringInfo(ri); // Open 'localization to other languages' window ScriptHelper.RegisterStartupScript(this, typeof(string), "OpenLocalization", ScriptHelper.GetScript("modalDialog('" + ResolveUrl(LOCALIZE_STRING) + "?hiddenValueControl=" + hdnValue.ClientID + "&stringKey=" + ri.StringKey + "&parentTextbox=" + textbox.ClientID + "', 'localizableString', 600, 635, null, null, true);")); // Set macro settings Value = MACRO_START + hdnValue.Value + MACRO_END; Reload(); } else { lblError.Visible = true; lblError.ResourceString = "localize.entertext"; } }
protected void Page_Load(object sender, EventArgs e) { // Check permissions if (CMSContext.CurrentUser.IsAuthorizedPerResource("CMS.Localization", "LocalizeStrings")) { // Set title CurrentMaster.Title.TitleText = GetString("localizable.localizefield"); CurrentMaster.Title.TitleImage = GetImageUrl("Objects/CMS_UICulture/new.png"); CurrentMaster.Title.HelpTopicName = "localize_field"; CurrentMaster.Title.HelpName = "helpTopic"; // Validate hash Regex re = RegexHelper.GetRegex(@"[\w\d_$$]*"); identificator = QueryHelper.GetString("params", ""); if (!QueryHelper.ValidateHash("hash") || !re.IsMatch(identificator)) { pnlContent.Visible = false; return; } // Load dialog parameters Hashtable parameters = (Hashtable)WindowHelper.GetItem(identificator.ToString()); if (parameters != null) { hdnValue = ValidationHelper.GetString(parameters["HiddenValue"], String.Empty); textbox = ValidationHelper.GetString(parameters["TextBoxID"], String.Empty); hdnIsMacro = ValidationHelper.GetString(parameters["HiddenIsMacro"], String.Empty); plainText = ValidationHelper.GetString(parameters["TextBoxValue"], String.Empty); btnLocalizeField = ValidationHelper.GetString(parameters["ButtonLocalizeField"], String.Empty); btnLocalizeString = ValidationHelper.GetString(parameters["ButtonLocalizeString"], String.Empty); btnRemoveLocalization = ValidationHelper.GetString(parameters["ButtonRemoveLocalization"], String.Empty); resourceKeyPrefix = ValidationHelper.GetString(parameters["ResourceKeyPrefix"], String.Empty); } btnOk.Click += new EventHandler(btnOk_Click); lstExistingOrNew.Items[0].Text = GetString("localizable.createnew"); lstExistingOrNew.Items[1].Text = GetString("localizable.useexisting"); // Disable option to use existing resource string for user who is not admin if (!CurrentUser.UserSiteManagerAdmin) { lstExistingOrNew.Items[1].Enabled = false; } // If "create new" is selected if (lstExistingOrNew.SelectedIndex == 0) { if (!String.IsNullOrEmpty(resourceKeyPrefix)) { lblPrefix.Text = resourceKeyPrefix; lblPrefix.Visible = true; } lblSelectKey.ResourceString = GetString("localizable.newkey"); resourceSelector.Visible = false; txtNewResource.Visible = true; if (!RequestHelper.IsPostBack()) { // Get maximum length of resource key int maxKeyLength = MAX_KEY_LENGTH; string keyPrefix = resourceKeyPrefix; if (SettingsKeyProvider.DevelopmentMode && String.IsNullOrEmpty(keyPrefix)) { keyPrefix = PREFIX; } if (!String.IsNullOrEmpty(keyPrefix)) { maxKeyLength -= keyPrefix.Length; } // Initialize resource string string newResource = TextHelper.LimitLength(ValidationHelper.GetCodeName(plainText), maxKeyLength, String.Empty, true); // Check if key already exists string key = keyPrefix + newResource; int i = 0; // If key exists then create new one with number as suffix while (SqlResourceManager.GetResourceStringInfo(key) != null) { // If newly created resource key is longer then allowed length then trim end by one character if ((keyPrefix.Length + newResource.Length + ++i) > MAX_KEY_LENGTH) { newResource = newResource.Substring(0, newResource.Length - 1); } key = keyPrefix + newResource + i; } // Set newly created resource string if (String.IsNullOrEmpty(resourceKeyPrefix)) { if (!newResource.StartsWith(PREFIX)) { txtNewResource.Text = PREFIX + newResource; } else { txtNewResource.Text = newResource; } } else { txtNewResource.Text = newResource; } if (i > 0) { txtNewResource.Text += i; } } } // If "use existing" is selected else { lblSelectKey.ResourceString = GetString("localizable.existingkey"); resourceSelector.Visible = true; txtNewResource.Visible = false; } } // Dialog is not available for unauthorized user else { lblError.ResourceString = "security.accesspage.onlyglobaladmin"; lblError.Visible = true; pnlControls.Visible = false; } }
/// <summary> /// Button OK clicked. /// </summary> void btnOk_Click(object sender, EventArgs e) { // Check permissions if (CMSContext.CurrentUser.IsAuthorizedPerResource("CMS.Localization", "LocalizeStrings")) { string key = null; ResourceStringInfo ri = null; // Check if current user's culture exists UICultureInfo uiCulture = null; string cultureCode = CultureHelper.PreferredUICulture; try { uiCulture = UICultureInfoProvider.GetUICultureInfo(CultureHelper.PreferredUICulture); } // Use default UI culture catch { cultureCode = CultureHelper.DefaultUICulture; } // Use default UI culture if (uiCulture == null) { cultureCode = CultureHelper.DefaultUICulture; } // Creating new resource string if (lstExistingOrNew.SelectedIndex == 0) { if (SettingsKeyProvider.DevelopmentMode && String.IsNullOrEmpty(resourceKeyPrefix)) { key = txtNewResource.Text.Trim(); } else { key = resourceKeyPrefix + txtNewResource.Text.Trim(); } ri = SqlResourceManager.GetResourceStringInfo(key); // Resource string doesn't exists yet if (ri == null) { lblError.Text = new Validator().NotEmpty(key, GetString("Administration-UICulture_String_New.EmptyKey")).IsCodeName(key, GetString("Administration-UICulture_String_New.InvalidCodeName")).Result; if (!String.IsNullOrEmpty(lblError.Text)) { lblError.Visible = true; } else { // Save ResourceString ri = new ResourceStringInfo(); ri.StringKey = key; ri.UICultureCode = cultureCode; ri.TranslationText = plainText; ri.StringIsCustom = !SettingsKeyProvider.DevelopmentMode; SqlResourceManager.SetResourceStringInfo(ri); ScriptHelper.RegisterStartupScript(this, typeof(string), "localizeField", ScriptHelper.GetScript("wopener.SetResourceAndOpen('" + hdnValue + "', '" + key + "', '" + textbox + "', " + ScriptHelper.GetString(plainText) + ", '" + hdnIsMacro + "', '" + btnLocalizeField + "', '" + btnLocalizeString + "', '" + btnRemoveLocalization + "', window);")); } } // If resource string already exists with different translation else { lblError.Visible = true; lblError.ResourceString = "localize.alreadyexists"; } } // Using existing resource string else { key = ValidationHelper.GetString(resourceSelector.Value, String.Empty).Trim(); ri = SqlResourceManager.GetResourceStringInfo(key); // Key not found in DB if (ri == null) { // Try to find it in .resx file and save it in DB FileResourceManager resourceManager = ResHelper.GetFileManager(cultureCode); if (resourceManager != null) { string translation = resourceManager.GetString(key); if (!String.IsNullOrEmpty(translation)) { ri = new ResourceStringInfo(); ri.StringKey = key; ri.StringIsCustom = !SettingsKeyProvider.DevelopmentMode; ri.UICultureCode = cultureCode; ri.TranslationText = translation; SqlResourceManager.SetResourceStringInfo(ri); ScriptHelper.RegisterStartupScript(this, typeof(string), "localizeField", ScriptHelper.GetScript("wopener.SetResource('" + hdnValue + "', '" + key + "', '" + textbox + "', " + ScriptHelper.GetString(translation) + ", '" + hdnIsMacro + "', '" + btnLocalizeField + "', '" + btnLocalizeString + "', '" + btnRemoveLocalization + "', window);")); } else { lblError.Visible = true; lblError.ResourceString = "localize.doesntexist"; } } else { lblError.Visible = true; lblError.ResourceString = "localize.doesntexist"; } } // Send to parent window selected resource key else { string existingTranslation = GetString(key); ScriptHelper.RegisterStartupScript(this, typeof(string), "localizeField", ScriptHelper.GetScript("wopener.SetResource('" + hdnValue + "', '" + key + "', '" + textbox + "', " + ScriptHelper.GetString(existingTranslation) + ", '" + hdnIsMacro + "', '" + btnLocalizeField + "', '" + btnLocalizeString + "', '" + btnRemoveLocalization + "', window);")); } } } else { lblError.ResourceString = "general.actiondenied"; lblError.Visible = true; pnlControls.Visible = false; } }
protected void Page_Load(object sender, EventArgs e) { // Get parameters from query string GetParameters(); if (saved) { ShowChangesSaved(); } lblEnglishText.Text = string.Format(GetString("Administration-UICulture_String_New.EnglishText"), CultureHelper.DefaultUICulture); rfvKey.ErrorMessage = GetString("Administration-UICulture_String_New.EmptyKey"); ResourceStringInfo ri = SqlResourceManager.GetResourceStringInfo(stringID, uiCultureID); EditedObject = ri; string defaultCulture = CultureHelper.DefaultUICulture; uic = UICultureInfoProvider.GetUICultureInfo(uiCultureID); if (uic.UICultureCode == defaultCulture) { // Default culture plcDefaultText.Visible = false; txtKey.Visible = true; lblKeyEng.Visible = false; if (!RequestHelper.IsPostBack()) { txtKey.Text = ri.StringKey; txtText.Text = SqlResourceManager.GetStringStrictly(ri.StringKey, CultureHelper.DefaultUICulture); } } else { // Other cultures plcDefaultText.Visible = true; txtKey.Visible = false; rfvKey.Enabled = false; lblKeyEng.Visible = true; lblKeyEng.Text = ri.StringKey; lblEnglishValue.Text = HTMLHelper.HTMLEncode(MacroResolver.RemoveSecurityParameters(SqlResourceManager.GetStringStrictly(ri.StringKey, CultureHelper.DefaultUICulture), true, null)); if (!RequestHelper.IsPostBack()) { txtKey.Text = ri.StringKey; txtText.Text = SqlResourceManager.GetStringStrictly(ri.StringKey, uic.UICultureCode); } // Set default culture text to translate txtText.AllowTranslationServices = true; txtText.TranslationSourceText = ResHelper.GetString(ri.StringKey, defaultCulture); txtText.TranslationSourceLanguage = defaultCulture; txtText.TranslationTargetLanguage = uic.UICultureCode; } if (!DialogMode) { // Initialize master page InitializeMasterPage(ri, plcDefaultText.Visible); } else { txtKey.Enabled = false; plcCustom.Visible = false; } if (!RequestHelper.IsPostBack() && (!DialogMode)) { chkCustomString.Checked = ri.StringIsCustom; } }
protected void btnOK_Click(object sender, EventArgs e) { // History back count BackCount++; string result = null; // Trim the key before save string key = txtKey.Text.Trim(); // Validate the code name if default culture if (uic.UICultureCode == CultureHelper.DefaultUICulture) { result = new Validator() .NotEmpty(key, rfvKey.ErrorMessage) .IsCodeName(key, GetString("Administration-UICulture_String_New.InvalidCodeName")) .Result; } if (!string.IsNullOrEmpty(result)) { // Display error message ShowError(result); return; } // Update the string ResourceStringInfo ri = SqlResourceManager.GetResourceStringInfo(stringID, uiCultureID); if (ri != null) { // Check if string with given key is not already defined ResourceStringInfo existing = SqlResourceManager.GetResourceStringInfo(key); if ((existing == null) || (existing.StringId == ri.StringId)) { ri.StringIsCustom = chkCustomString.Checked; ri.UICultureCode = uic.UICultureCode; ri.TranslationText = txtText.Text; if (txtKey.Visible) { // If key changed, log deletion of old string string newKey = key; if ((!CMSString.Equals(ri.StringKey, newKey, true)) && (ri.Generalized.LogSynchronization == SynchronizationTypeEnum.LogSynchronization)) { SynchronizationHelper.LogObjectChange(ri, TaskTypeEnum.DeleteObject); } ri.StringKey = key; } // Update key SqlResourceManager.SetResourceStringInfo(ri); ShowChangesSaved(); tabs[1, 0] = ri.StringKey; } else { ShowError(string.Format(GetString("Administration-UICulture_String_New.StringExists"), key)); } } }
/// <summary> /// Saves resource translations and returns TRUE if save was successful. Returns FALSE if any error ocurred. /// </summary> private void Save() { // Check permissions if (CMSContext.CurrentUser.IsAuthorizedPerResource("CMS.Localization", "LocalizeStrings")) { // Change resource key string key = txtStringKey.Text.Trim(); if (key != rsi.StringKey) { // Validate the key string result = new Validator().NotEmpty(key, rfvKey.ErrorMessage).IsCodeName(key, GetString("Administration-UICulture_String_New.InvalidCodeName")).Result; if (String.IsNullOrEmpty(result)) { ResourceStringInfo riNew = SqlResourceManager.GetResourceStringInfo(txtStringKey.Text.Trim()); // Check if string with given key is not already defined if ((riNew == null) || (rsi.StringId == riNew.StringId)) { // Log deletion of old string if ((!String.Equals(key, rsi.StringKey, StringComparison.OrdinalIgnoreCase)) && (rsi.Generalized.LogSynchronization == SynchronizationTypeEnum.LogSynchronization)) { SynchronizationHelper.LogObjectChange(rsi, TaskTypeEnum.DeleteObject); } rsi.StringKey = key; SqlResourceManager.SetResourceStringInfo(rsi); } // New resource key collides with already existing resource key else { lblError.Text = String.Format(GetString("Administration-UICulture_String_New.StringExists"), key); lblError.Visible = true; } } // New resource string key is not code name else { lblError.Visible = true; lblError.Text = result; } } string existingTranslation = null; string newTranslation = null; FormEngineUserControl control = null; // Go through all cultures foreach (string cultureCode in translations.Keys) { // Check if translation in given culture exists existingTranslation = SqlResourceManager.GetStringStrictly(txtStringKey.Text, cultureCode); // Get control for given culture control = (FormEngineUserControl)translations[cultureCode]; if (control != null) { // Translation is not already created if (String.IsNullOrEmpty(existingTranslation)) { // Get new translation newTranslation = ValidationHelper.GetString(control.Value, String.Empty).Trim(); // Create new translation in given culture if (!String.IsNullOrEmpty(newTranslation)) { UpdateString(cultureCode, newTranslation); } // Translation of default culture must exist else if (cultureCode == CultureHelper.DefaultUICulture) { lblError.Text = String.Format(ResHelper.GetString("localizable.deletedefault"), defaultCultureName); lblError.Visible = true; } } // Existing translation is being updated else { newTranslation = ValidationHelper.GetString(control.Value, String.Empty).Trim(); // Delete translation if new translation is empty if (String.IsNullOrEmpty(newTranslation)) { // Delete translation if (cultureCode != CultureHelper.DefaultUICulture) { SqlResourceManager.DeleteResourceStringInfo(txtStringKey.Text, cultureCode); } // Translation in default culture cannot be deleted or set to empty in Localizable textbox else { lblError.Text = String.Format(ResHelper.GetString("localizable.deletedefault"), defaultCultureName); lblError.Visible = true; } } // Update translation if new translation is not empty else { UpdateString(cultureCode, newTranslation); } } // Set updated translation in current culture if (cultureCode == CultureHelper.PreferredUICulture) { defaultTranslation = newTranslation; } } } } // Current user is not global admin else { lblError.ResourceString = "general.actiondenied"; lblError.Visible = true; pnlControls.Visible = false; } }
/// <summary> /// Reloads data. /// </summary> protected void ReloadData() { if (!RequestHelper.IsPostBack()) { string key = QueryHelper.GetString("stringKey", "").Trim().Replace("'", "''");; txtStringKey.Text = key; rsi = SqlResourceManager.GetResourceStringInfo(txtStringKey.Text); // Ensure the value if not found if (rsi == null) { string currentCulture = CultureHelper.PreferredUICulture; string defaultCulture = CultureHelper.DefaultUICulture; string value = ResHelper.GetString(key, defaultCulture, NOT_FOUND); if (value != NOT_FOUND) { // Create the resource string with current culture value rsi = new ResourceStringInfo(); rsi.StringKey = key; rsi.StringIsCustom = true; rsi.UICultureCode = defaultCulture; rsi.TranslationText = value; SqlResourceManager.SetResourceStringInfo(rsi); // Impor the current culture if (!currentCulture.Equals(defaultCulture, StringComparison.InvariantCultureIgnoreCase)) { rsi.UICultureCode = currentCulture; rsi.TranslationText = ResHelper.GetString(key, currentCulture); SqlResourceManager.SetResourceStringInfo(rsi); } } } } else { rsi = SqlResourceManager.GetResourceStringInfo(ValidationHelper.GetInteger(hdnID.Value, 0)); } if (rsi != null) { if (!RequestHelper.IsPostBack()) { chkIsCustom.Checked = rsi.StringIsCustom; hdnID.Value = rsi.StringId.ToString(); } // Get the cultures DataSet result = GetData(); DataSet defaultCulture = GetDefaultCulture(result); if (!DataHelper.DataSourceIsEmpty(result) || !DataHelper.DataSourceIsEmpty(defaultCulture)) { int rowCount = 1; TableRow row = null; TableCell cellText = null; TableCell cellValue = null; FormEngineUserControl control = null; Control c = null; // Add default culture translation as a first record if (!DataHelper.DataSourceIsEmpty(defaultCulture)) { AddRow(true, ref rowCount, row, cellText, cellValue, control, c, defaultCulture.Tables[0].Rows[0]); } // Add all cultures if (!DataHelper.DataSourceIsEmpty(result)) { foreach (DataRow dr in result.Tables[0].Rows) { AddRow(false, ref rowCount, row, cellText, cellValue, control, c, dr); } } // Display filter for large number of results if ((rowCount > 6) || (txtFilter.Text.Trim().Length != 0)) { pnlHeaderCell.Visible = true; lblHeaderCell.Visible = false; } else { pnlHeaderCell.Visible = false; lblHeaderCell.Visible = true; } } else { if (txtFilter.Text.Trim().Length != 0) { pnlHeaderCell.Visible = true; lblHeaderCell.Visible = false; } else { pnlHeaderCell.Visible = false; lblHeaderCell.Visible = true; } } } else { lblError.ResourceString = "resourcestring.notfound"; lblError.Visible = true; pnlControls.Visible = false; btnApply.Enabled = false; btnOk.Enabled = false; } }