1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Xml.Linq;
- using GCAS.Dto;
- namespace GCAS.Localization
- {
- internal static class LocalizationHelper
- {
- public static List<NameValue> GetSource(CultureInfo cultureInfo)
- {
- var xmlSoruce = Properties.Resources.Languages;
- XDocument xdoc = XDocument.Parse(xmlSoruce);
- var culture = xdoc.Root.Elements("Language").SingleOrDefault(c => c.Attribute("culture").Value == cultureInfo.Name);
- if (culture != null)
- {
- var texts = culture.Element("texts")
- .Elements("text")
- .Select(c => new NameValue
- {
- Name = c.Attribute("name").Value,
- Value = c.Value
- }).ToList();
- return texts;
- }
- else
- {
- throw new Exception("未找到资源名为: " + cultureInfo.Name + " 的资源包!");
- }
- }
- public static string GetString(this List<NameValue> source, string name)
- {
- if (source != null && source.Any())
- {
- var value = source.SingleOrDefault(c => c.Name.ToLower() == name.ToLower());
- if (value == null)
- {
- return name;
- }
- else
- {
- return value.Value;
- }
- }
- throw new Exception("资源文件为空!");
- }
- }
- }
|