如何建立翻译功能

时间:2018-01-11 08:22:02

标签: c#-4.0

我想建立翻译功能,例如" a.b.c.Password"。如果我想翻译像"密码"到其他语言。就像" Password_FR"。 " A.B.C"是名称空间和"密码"是翻译文本。第二个"密码"是翻译项目。所以现在我没有理想。这是守则。

public class NamespaceManager : INamespace
{
    public readonly static NamespaceManager Root = new NamespaceManager();

    private Dictionary<IText, ITextItem> Dictionary;
    private InternalNamespace root;
    private NamespaceManager()
    {
        this.root = new InternalNamespace(string.Empty, null);
        Dictionary = new Dictionary<IText, ITextItem>();
    }

    public INamespace GetOrAddNamespace(string @namespace)
    {
        string[] names = @namespace.Split(".");
        return names.Aggregate(this.root, (p, v) => p.GetOrAdd(v));
    }

    public INamespace this[string name] => this.root[name];

    public string Name => this.root.Name;

    public string FullName => this.root.FullName;

    public INamespace Parent => this.root.Parent;

    public IEnumerator<INamespace> GetEnumerator() => this.root.GetEnumerator();

    IEnumerator IEnumerable.GetEnumerator() => this.root.GetEnumerator();

    public IText AddText(string textID, string defaultText)
    {
        return this.root.AddText(textID, defaultText);
    }

    public bool ContainsTextID(string textID)
    {
        return this.root.ContainsTextID(textID);
    }

    private class InternalNamespace : INamespace
    {
        private List<InternalNamespace> childs;
        private Dictionary<string, IText> textDictionary;
        private InternalText internalText;


        public InternalNamespace(string name, INamespace parent)
        {
            this.Name = name;
            this.Parent = parent;
            this.childs = new List<InternalNamespace>();
            this.textDictionary = new Dictionary<string, IText>();
        }

        public bool IsRootNamespace() => string.IsNullOrWhiteSpace(this.Name) && null == this.Parent;

        public INamespace this[string name] => this.TryGet(name, out InternalNamespace @namespace) ? @namespace : null;

        public string Name { get; set; }

        public string FullName => null == this.Parent ? this.Name : $"{this.Parent.FullName}.{this.Name}";

        public INamespace Parent { get; set; }

        public IEnumerator<INamespace> GetEnumerator() => this.childs.GetEnumerator();

        public InternalNamespace GetOrAdd(string name)
        {
            if(!this.TryGet(name, out InternalNamespace @namespace))
            {
                InternalNamespace parent = this.IsRootNamespace() ? null : this;
                @namespace = new InternalNamespace(name, parent);
                this.childs.Add(@namespace);
            }

             return @namespace;
        }

        public bool TryGet(string name, out InternalNamespace @namespace)
        {
            @namespace = this.childs.SingleOrDefault(p => string.Compare(name, p.Name) == 0);
            return null != @namespace;
        }

        IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();

        public IText AddText(string textID, string defaultText)
        {

            if (ContainsTextID(textID))
            {
                throw new Exception();
            }
            else
            {
                internalText = new InternalText(textID,defaultText,this);
                internalText.FullName = $"{this.FullName}.{internalText.Name}";  
                textDictionary.Add(textID, internalText);
            }
            return internalText;
        }

        public bool ContainsTextID(string textID)
        {
            return this.textDictionary.ContainsKey(textID);
        }
    }

    public string Translate(string text, string language)
    {
        string[] tmp = text.Split(".");
        IText t = this.Dictionary.GetValueOrDefault()
    }

    public void SaveText(string @namespace, string text, string defaultText, Dictionary<string, string> dict)
    { 
        IText t = this.GetOrAddNamespace(@namespace).AddText(text, defaultText);
        foreach (var k in dict.Keys)
            t.AddItem(k, dict[k]);
    }

    private class InternalText : IText
    {
        public string Name { get; set; }

        public string DefaultText { get; set; }

        public string FullName { get; set; }

        public INamespace Namespace { get; set; }

        private List<InternalItem> listitem;

        private InternalItem internalItem;

        public InternalText()
        {
            this.listitem = new List<InternalItem>();
        }

        public InternalText(string textname,string defaultText,InternalNamespace internalNamespace)
        {
            Name = textname;
            DefaultText = defaultText;
            this.Namespace = internalNamespace;
            this.listitem = new List<InternalItem>();
        }

        public string this[string language]
        {
            get
            {
                return this.listitem.SingleOrDefault(p=>p.Language == language).Item;
            }
            set
            {
                if (!listitem.Any(p=>p.Language == language))
                {
                    this.AddItem(language, value);
                }
                else
                {
                    this.listitem.SingleOrDefault(p => p.Language == language).Item = value;
                }

            }
        }

        public ITextItem AddItem(string language, string item)
        {
            if (listitem.Any(p => p.Language == language && p.Text == this))
            {
                throw new Exception();
            }
            else
            {
                internalItem = new InternalItem(language, item);
                internalItem.Text = this;
                listitem.Add(internalItem);
                return internalItem;
            }
        }
    }


    private class InternalItem : ITextItem
    {
        public string Language { get; set; }
        public string Item { get; set; }
        public IText Text { get; set; }

        public InternalItem(string language, string item)
        {
            this.Language = language;
            this.Item = item;
        }
        public InternalItem()
        { }

    }

}

这是界面

public interface INamespace : IEnumerable<INamespace>
{
    string Name { get; }
    string FullName { get; }
    INamespace Parent { get; }
    INamespace this[string name] { get; }

    IText AddText(string textID, string defaultText);
    bool ContainsTextID(string textID);

}

public interface IText
{
    string Name { get; }
    string DefaultText { get; }
    string FullName { get; }
    INamespace Namespace { get; }
    String this[string language] { get; set; }
    ITextItem AddItem(string language, string item);
}

public interface ITextItem
{
    string Language { get; }
    string Item { get; set; }
    IText Text { get;  }

}

所以任何人都可以给我一些建议。谢谢你们。

0 个答案:

没有答案