// add all the .xaml dictionaries to the App.xaml resources public static void EnsureAppXaml(Project project = null) { try { if (project == null) { project = VsUtils.GetCurrentProject(); } var appXaml = VsUtils.GetFullPath(project, "App.xaml"); if (string.IsNullOrEmpty(appXaml)) { return; // should never happen... } var doc = XDocument.Load(appXaml); var appResources = getNode(doc.Root, "Application.Resources"); var resDictionary = getNode(appResources, "ResourceDictionary"); var mergedDictionaries = getNode(resDictionary, "ResourceDictionary.MergedDictionaries"); var resFolder = Resourcer.GetResourcesFolderPath(); if (Directory.Exists(resFolder)) { // clear all resources mergedDictionaries.RemoveAll(); // add them again foreach (var file in VsUtils.GetProjectItemsInFolder(project, resFolder)) { // perhaps it's not a dictionary (.xaml) file if (!file.Name.EndsWith(".xaml")) { continue; } // perhaps it is a folder if (!File.Exists(file.Properties.Item("FullPath").Value.ToString())) { continue; } mergedDictionaries.Add(new XElement( mergedDictionaries.Name.Namespace + "ResourceDictionary", new XAttribute("Source", Settings.ResourcesFolderName + "/" + file.Name) )); } } StringsXMLEditor.SaveDocument(doc, appXaml); } catch { /* nobody likes errors (which shouldn't happen) :( */ } }
void acceptB_Click(object sender, EventArgs e) { if (StringsXMLEditor.ContainsKey(resFile, name)) { int i = 0; while (StringsXMLEditor.ContainsKey(resFile, name + ++i)) { ; } name = name + i; } DialogResult = DialogResult.OK; }
// ensure the strings.xml file exists public static void EnsureStringResources(Project project = null) { if (project == null) { project = VsUtils.GetCurrentProject(); } var resFile = Resourcer.GetResourcePath(Resourcer.GetStringsResName()); if (!File.Exists(resFile)) // if the file doesn't exist, create it { StringsXMLEditor.CreateDocument(resFile); } VsUtils.AddFileIfUnexisting(VsUtils.GetCurrentProject(), resFile); }
// add a string to the strings resources // returns a call (as C# code) to retrieve the new added resource public static string AddString(KeyValuePair <string, string> keyValue, bool isXaml, bool resourceExisted, string locale = null) { Ensurer.EnsureStringResources(); // TODO no need to save if addstring document is used nowhere else... if (!resourceExisted) { var resFile = GetResourcePath(GetStringsResName(locale)); StringsXMLEditor.AddString(resFile, keyValue.Key, keyValue.Value); } Ensurer.EnsureEverything(); return(GetStringCall(keyValue.Key, isXaml)); }
void checkStatus() { if (name.Length == 0) { acceptB.Enabled = false; statusCSL.SetError("Please specify a resource name"); } else { acceptB.Enabled = true; if (StringsXMLEditor.ContainsKey(resFile, name)) { statusCSL.SetWarn("Resource name will be changed due to another already exists with that name"); } else { statusCSL.SetStatus("Ready to extract the string resource"); } } }
// update a string value public static void UpdateString(string key, string value, string locale) { var resFile = GetResourcePath(GetStringsResName(locale)); StringsXMLEditor.UpdateString(resFile, key, value); }
// update a string key for a given locale static void updateStringKey(string oldKey, string newKey, string locale) { var resFile = GetResourcePath(GetStringsResName(locale)); StringsXMLEditor.UpdateStringKey(resFile, oldKey, newKey); }
// get all the strings in a given locale public static Dictionary <string, string> GetStrings(string locale = null) { return(StringsXMLEditor.GetStrings(GetResourcePath(GetStringsResName(locale)))); }