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