Пример #1
0
        private void LoadLanguage()
        {
            if (Language == SystemLanguage.Undefined)
            {
                Language = (SystemLanguage)Enum.Parse(typeof(SystemLanguage),
                                                      GridConfig.Get("WGLanguage", SystemLanguage.English.ToString()),
                                                      true);
            }

            string cacheobjectSystemMessages = string.Format("{0}_{1}_{2}_SystemMessages_{3}", ClientID, Trace.ClientID, DataSourceId, Language);

            if (m_GridSystemMessages != null)
            {
                return;
            }
            if (DesignMode == false && HttpRuntime.Cache != null && Equals(CacheGridStructure, true) &&
                HttpRuntime.Cache.Get(cacheobjectSystemMessages) != null)
            {
                m_GridSystemMessages = HttpRuntime.Cache.Get(cacheobjectSystemMessages) as SystemMessages;

                if (Debug)
                {
                    m_DebugString.AppendFormat("<b>Cache</b> - Loading system messages from cache object: {0}<br/>",
                                               cacheobjectSystemMessages);
                }
                return;
            }

            if (Trace.IsTracing)
            {
                Trace.Trace("{0} : Started LoadLanguage()", ID);
            }
            // What language should we load?

            string systemmessagefile = SystemMessageDataFile;

            try
            {
                if (systemmessagefile != null &&
                    systemmessagefile.StartsWith("http://", StringComparison.OrdinalIgnoreCase) == false)
                {
                    if (Equals(DesignMode, true) && Site != null)
                    {
                        systemmessagefile = GridConfig.LoadSystemLanguages(systemmessagefile, Site);
                    }
                    else
                    {
                        systemmessagefile = GridConfig.LoadSystemLanguages(systemmessagefile);
                    }
                }
                if (systemmessagefile != null && System.IO.File.Exists(systemmessagefile) == false)
                {
                    throw new GridException(
                              string.Format("The system message file '{0}' does not exists.", systemmessagefile));
                }
                if (string.IsNullOrEmpty(systemmessagefile))
                // Load from resources if not available.
                {
                    m_GridSystemMessages = new SystemMessages();
                    Assembly a = Assembly.GetExecutingAssembly(); //Assembly.Load(GetType().Assembly.GetName().Name);

                    Stream str = a.GetManifestResourceStream("WebGrid.Resources.WebGridMessages.xml");
                    if (str == null)
                    {
                        throw new GridException("WebGrid messages is not found in resources.");
                    }
                    XmlTextReader tr  = new XmlTextReader(str);
                    XmlDocument   xml = new XmlDocument();
                    xml.Load(tr);
                    XPathNavigator nav = xml.CreateNavigator();

                    if (nav == null)
                    {
                        throw new GridException("Unable to get a XpathNavigator for WebGrid Messages (in resources).");
                    }
                    XPathNodeIterator it = nav.Select(string.Format(@"//{0}/*", Language));

                    while (it.MoveNext())
                    {
                        if (string.IsNullOrEmpty(it.Current.Name) || string.IsNullOrEmpty(it.Current.Value))
                        {
                            continue;
                        }
                        m_GridSystemMessages.SetSystemMessage(it.Current.Name, it.Current.Value, null);
                    }
                }
                else
                {
                    m_GridSystemMessages = new SystemMessages(Language, systemmessagefile, this);
                }
            }
            catch (Exception ee)
            {
                throw new GridException(
                          string.Format("Error loading WebGrid system message file '{0}'.", systemmessagefile), ee);
            }

            if (HttpRuntime.Cache != null && Equals(CacheGridStructure, true))
            {
                HttpRuntime.Cache[cacheobjectSystemMessages] = m_GridSystemMessages;
            }
            if (Trace.IsTracing)
            {
                Trace.Trace(string.Format("{0} : Stopped LoadLanguage()", ID));
            }
        }