public void GetCultureInfoFromFileNameTest() { var extractor = new ResxExtractCultureFromFileStrategy(); // typical case (name + culture + extension) Assert.AreEqual("el", extractor.GetCulture("Part1.el.xml")); Assert.AreEqual("el-GR", extractor.GetCulture("Part1.el-GR.xml")); // typical case (name + culture + extension) Assert.AreEqual("el", extractor.GetCulture("Part1.el.resx")); Assert.AreEqual("el-GR", extractor.GetCulture("Part1.el-GR.resx")); // typical case (nameWithDots + culture + extension) Assert.AreEqual("el", extractor.GetCulture("Part1.Part2.el.resx")); Assert.AreEqual("el-GR", extractor.GetCulture("Part1.Part2.el-GR.resx")); // case with four letters in country name (zh-Hans) Assert.AreEqual("zh-Hans", extractor.GetCulture("Part1.Part2.zh-Hans.resx")); // case with 2 dashes in culture (ha-Latn-NG) ////Assert.AreEqual("ha-Latn-NG", extractor.GetCulture("Part1.Part2.ha-Latn-NG.resx")); // file with no culture information Assert.AreEqual(ResourceSet.NeutralCulture, extractor.GetCulture("Part1.resx")); Assert.AreEqual(ResourceSet.NeutralCulture, extractor.GetCulture("Part1.Part2.resx")); }
public virtual void Save(string fileName, ResourceBundle resourceBundle) { var baseFileInfo = new FileInfo(fileName); string basePath = baseFileInfo.DirectoryName ?? string.Empty; // if extension of input file is not .resx and an xslt file to convert an xml into valid resx exists, then use it // else consider that the input file is .resx already string xsltFile = string.Empty; if (!string.Equals(baseFileInfo.Extension, ".resx", StringComparison.InvariantCultureIgnoreCase)) { var xsltFullPath = Path.Combine(basePath, xsltConvertFromResx); xsltFile = FileSystem.FileExists(xsltFullPath) ? xsltFullPath : null; } var extractCultureFromFile = new ResxExtractCultureFromFileStrategy(); // remove culture information from base file name // (eg. if MyFile.en.resx then remove en and keep MyFile..resx) string baseName = baseFileInfo.Name; var cultureOfInputFile = extractCultureFromFile.GetCulture(baseFileInfo.Name); if (cultureOfInputFile != ResourceSet.NeutralCulture) { // if input file contains culture information, then remove it baseName = extractCultureFromFile.ReplaceCulture(baseName, string.Empty); } // remove extension from the base file name baseName = Path.GetFileNameWithoutExtension(baseName); // remove any trailing dots from the base file name // (this can happen if base file name was containing culture name which // was removed a few lines above) baseName = baseName.TrimEnd('.'); // create a array instead of enumerating original collection, // since the code bellow may delete items from the that collection and would break the foreach process var resourceSets = resourceBundle.ToArray(); foreach (var resourceSet in resourceSets) { string culture = resourceSet.Culture == ResourceSet.NeutralCulture ? string.Empty : "." + resourceSet.Culture; string resourceSetFullFileName = Path.Combine(basePath, baseName + culture + baseFileInfo.Extension); switch (resourceSet.Status) { case ResourceSetStatus.Deleted: // delete file from disk File.Delete(resourceSetFullFileName); // remove from collection resourceBundle.Remove(resourceSet); break; case ResourceSetStatus.New: case ResourceSetStatus.Updated: // save to resource file ResourceSet2Resx(resourceSet, resourceSetFullFileName, xsltFile); break; } } }
public virtual ResourceBundle Load(string fileName) { var baseFileInfo = new FileInfo(fileName); var basePath = baseFileInfo.DirectoryName ?? string.Empty; // if extension of input file is not .resx and an xslt file to convert an xml into valid resx exists, then use it // else consider that the input file is .resx already string xsltFile = string.Empty; if (!string.Equals(baseFileInfo.Extension, ".resx", StringComparison.InvariantCultureIgnoreCase)) { var xsltFullPath = Path.Combine(basePath, xsltConvertToResx); xsltFile = FileSystem.FileExists(xsltFullPath) ? xsltFullPath : null; } var extractCultureFromFile = new ResxExtractCultureFromFileStrategy(); // define the search patterh (eg. MyFile.*.resx) that will be used to detect files of // other cultures, given the input file string searchPattern = fileName; var cultureOfInputFile = extractCultureFromFile.GetCulture(baseFileInfo.Name); if (cultureOfInputFile != ResourceSet.NeutralCulture) { // if input file contains culture information, then remove it searchPattern = extractCultureFromFile.ReplaceCulture(searchPattern, string.Empty); baseFileInfo = new FileInfo(searchPattern); } // put a wildcard before the extension searchPattern = Path.GetFileNameWithoutExtension(searchPattern) + "*" + baseFileInfo.Extension; var resourceBundle = new ResourceBundle(); foreach (string file in this.FileSystem.GetFiles(basePath, searchPattern, SearchOption.TopDirectoryOnly)) { try { if (baseFileInfo.FullName == extractCultureFromFile.ReplaceCulture(file, string.Empty)) { var culture = extractCultureFromFile.GetCulture(file); // If neutral culture was detected for the second time, we are skipping the file // There are two reasons that this can happen: // a) The ResxExtractCultureFromFileStrategy failed to detect a know culture the file name and returned neutral // b) File name is not part of the same resource bundle but happens to have similar name that pas the same prefix and .resx extension // eg. our main file name could be MyFile.resx and this file name could be MyFile.Version2.resx if (culture == ResourceSet.NeutralCulture && resourceBundle.ContainsCulture(culture)) { continue; } var resourceSet = new ResourceSet(culture); Resx2ResourceSet(resourceSet, file, xsltFile); resourceBundle.Add(resourceSet); } } catch (Exception ex) { throw new InvalidOperationException(string.Format("Failed to open file '{0}'", file), ex); } } return(resourceBundle); }