How to use localization in C#

后端 未结 9 2771
栀梦
栀梦 2020-11-22 02:59

I just can\'t seem to get localization to work.

I have a class library. Now I want to create resx files in there, and return some values based on the threa

9条回答
  •  情歌与酒
    2020-11-22 03:12

    ResourceManager and .resx are bit messy.

    You could use Lexical.Localization¹ which allows embedding default value and culture specific values into the code, and be expanded in external localization files for futher cultures (like .json or .resx).

    public class MyClass
    {
        /// 
        /// Localization root for this class.
        /// 
        static ILine localization = LineRoot.Global.Type();
    
        /// 
        /// Localization key "Ok" with a default string, and couple of inlined strings for two cultures.
        /// 
        static ILine ok = localization.Key("Success")
                .Text("Success")
                .fi("Onnistui")
                .sv("Det funkar");
    
        /// 
        /// Localization key "Error" with a default string, and couple of inlined ones for two cultures.
        /// 
        static ILine error = localization.Key("Error")
                .Format("Error (Code=0x{0:X8})")
                .fi("Virhe (Koodi=0x{0:X8})")
                .sv("Sönder (Kod=0x{0:X8})");
    
        public void DoOk()
        {
            Console.WriteLine( ok );
        }
    
        public void DoError()
        {
            Console.WriteLine( error.Value(0x100) );
        }
    }
    

    ¹ (I'm maintainer of that library)

提交回复
热议问题