Ejemplo n.º 1
0
        /// <summary>
        /// Loads the element data from file and creates a new periodic table.
        /// </summary>
        /// <returns>A periodic table object with the loaded data.</returns>
        public static PeriodicTable Load()
        {
            var dict = new Dictionary <string, Element>();

            string[] symbols  = new string[118];
            Assembly assembly = typeof(PeriodicTable).GetTypeInfo().Assembly;

            using (StreamReader sr = new StreamReader(assembly.GetManifestResourceStream("Bluegrams.Periodica.Data.Data.ElementData.csv")))
            {
                CsvReader reader = new CsvReader(sr);
                reader.Configuration.CultureInfo = CultureInfo.InvariantCulture;
                reader.Configuration.RegisterClassMap <ElementClassMap>();
                var items = reader.GetRecords <Element>();
                foreach (Element item in items)
                {
                    dict.Add(item.Symbol, item);
                    symbols[item.AtomicNumber - 1] = item.Symbol;
                }
            }
            PeriodicTable table = new PeriodicTable()
            {
                Elements = dict, ElementSymbols = symbols
            };

            return(table);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads the element data from file and applies the specified translation.
        /// </summary>
        /// <param name="localization">The culture info of the localization that should be loaded.</param>
        /// <returns>A localized periodic table object with the loaded data.</returns>
        public static PeriodicTable Load(CultureInfo localization)
        {
            PeriodicTable table = Load();

            table.Localization = localization;
            if (localization.TwoLetterISOLanguageName == "iv")
            {
                return(table);
            }
            // Check if localization is supported.
            Assembly assembly   = typeof(PeriodicTable).GetTypeInfo().Assembly;
            string   streamName = String.Format("Bluegrams.Periodica.Data.Data.ElementData_{0}.csv",
                                                localization.TwoLetterISOLanguageName);
            bool hasSupport = Array.IndexOf(assembly.GetManifestResourceNames(), streamName) > -1;

            // Fall back to english if localization is not supported.
            if (localization.TwoLetterISOLanguageName == "en" || !hasSupport)
            {
                foreach (Element elem in table)
                {
                    elem.LocalizedName = elem.EnglishName;
                }
                return(table);
            }
            // Read local names from stream.
            using (StreamReader sr = new StreamReader(assembly.GetManifestResourceStream(streamName)))
            {
                CsvReader reader = new CsvReader(sr);
                while (reader.Read())
                {
                    var symbol = reader.GetField("Symbol");
                    var name   = reader.GetField("LocalName");
                    table[symbol].LocalizedName = name;
                }
            }
            return(table);
        }