开发者

.NET windows WPF LOCALIZATION

开发者 https://www.devze.com 2023-04-09 09:22 出处:网络
I am newto localization ... ..I initially created a resource file(.resx file) then used resgen command to generate corresponding .resource file.. it got created.. then I used al.exe to generate corres

I am new to localization ... ..I initially created a resource file(.resx file) then used resgen command to generate corresponding .resource file.. it got created.. then I used al.exe to generate corresponding satellite assembly.. it a开发者_高级运维lso got created .. but further when I tried to access .resx file from codebehind using Resource manager class it threw error something like--

Assembly Manifest exception ..

I am not understanding where I am wrong.. plz let me know if there is any further process to be done (plz don't suggest LOCBAML tool).. I want the solution using resource files only..


Your start was pretty good. Using LocBaml was too complicated for me too. So I had to figure out something easier for me. Here's solution: first, you have to create resource .resx file (this step is already done in your); Simple way to get all strings from resource file is to store it in dictionary. You can do it using this method:

public Dictionary<string, string> ApplicationStrings(string locale)
    {
        Dictionary<string, string> result = new Dictionary<string, string>();

        ResourceManager manager = null;
// Here is two locales (custom and english locale), but you can use more than two if there's a need
            if (locale == "Your locale")
            {
                manager = new ResourceManager(typeof(your_locale_resource_file));
            }
            else
            {
                manager = new ResourceManager(typeof(ApplicationStrings_en));
            }
        ResourceSet resources = manager.GetResourceSet(CultureInfo.CurrentCulture, true, true);

        IDictionaryEnumerator enumerator = resources.GetEnumerator();

        while (enumerator.MoveNext())
        {
            result.Add((string)enumerator.Key, (string)enumerator.Value);
        }

        return result;
    }

You have to set DataContext of MainWindow as this method result and bind all you strings to the dictionary. Binding example:

Text="{Binding [YourKey]}"

You can call this method and then change DataContext whenever and wherever you want. Thanks to Data Binding it works in runtime very well. I am guaranteed that this solution is not the best but it's working in a simple way. I hope it helps.


Have a look at these articles having details about using Resx files for localization in WPF -

Localizing a WPF Application with ResX Files

WPF Localization

I would also suggest you to go through these -

WPF Globalization and Localization Overview

WPF Localization Guidance - Whitepaper

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号