コード例 #1
0
        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;
                }
            }
        }