locked
Designer resx file remains empty after adding New Resource File RRS feed

  • Question

  • User1229570892 posted

    Hi There,

    I am working on a Globalization Project,

    For English Translation i have added default Resource.resx file.

    For other language(Japanese) i have added another resource file as Resource.ja.resx file.

    When i generate Designer file for the Japanese resource file(Resource.ja.resx) its remain empty

    Below Steps I have follwed to resolved but still i am unable to generate Designer files code.

    1) set CustomTools = PublicResXFileCodeGenerator and 

    Build Action = Embedded Resource in Resource.resx file property (Not working)

    2) When i Rename "Resource.ja.resx" to "Resource.ja1.resx" its able to generate Designer File with code , Which i thing is the not propper way to call resource.

    Monday, August 10, 2020 1:54 PM

All replies

  • User1686398519 posted

    Hi mishraashok5,

    1. Because the "ja" in the "Resource.ja.resx" is a specific culture specified in the CultureInfo class."ja1" is not a specific culture specified in the CultureInfo class, so the Designer.cs file will be created
    2. ASP.NET uses the resource file that best matches the setting of the CurrentUICulture property. The UI culture of the thread is set according to the UI culture of the page.
    3. For example, if the current UI culture is Japanese, it is ASP. NET uses a compiled version of the Resource.ja.resx file. If there is no match for the current UI culture, ASP.NET uses resource fallback. It starts by searching for resources for a specific culture. If those are not available, it searches for the resources for a neutral culture. If these are not found, ASP.NET loads the default resource file.The default resource file in this example is Resource.resx.
    4. Resource.ja.resx only needs to add name and value, and then it can be used. I made an example to help you understand the Resource File.

    Model

        public class test
        {
            [Display(Name = "hello", ResourceType = typeof(Resource))]
            public string Name{get;set;}
        }
        public class Languages
        {
            public string LanguageFullName { get; set; }
            public string LanguageCultureName { get; set; }
        }

    CommonController

            public class CommonController : Controller
            {
                protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
                {
                    string lang = null;
                    HttpCookie langCookie = Request.Cookies["culture"];
                    if (langCookie != null)
                    {
                        lang = langCookie.Value;
                    }
                    else
                    {
                        var userLanguage = Request.UserLanguages;
                        var userLang = userLanguage != null ? userLanguage[0] : "";
                        if (userLang != "")
                        {
                            lang = userLang;
                        }
                        else
                        {
                            lang = LanguageMang.GetDefaultLanguage();
                        }
                    }
                    new LanguageMang().SetLanguage(lang);
                    return base.BeginExecuteCore(callback, state);
                }        
        }

    LanguageMang

       public class LanguageMang
        {
            public static List<Languages> AvailableLanguages = new List<Languages> {
                new Languages {
                    LanguageFullName = "Defalut", LanguageCultureName = "en"
                },
                new Languages {
                    LanguageFullName = "Japanese", LanguageCultureName = "ja"
                }
            };
            public static bool IsLanguageAvailable(string lang)
            {
                return AvailableLanguages.Where(a => a.LanguageCultureName.Equals(lang)).FirstOrDefault() != null ? true : false;
            }
            public static string GetDefaultLanguage()
            {
                return AvailableLanguages[0].LanguageCultureName;
            }
            public void SetLanguage(string lang)
            {
                try
                {
                    if (!IsLanguageAvailable(lang)) lang = GetDefaultLanguage();
                    var cultureInfo = new CultureInfo(lang);
                    Thread.CurrentThread.CurrentUICulture = cultureInfo;
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
                    HttpCookie langCookie = new HttpCookie("culture", lang);
                    langCookie.Expires = DateTime.Now.AddYears(1);
                    HttpContext.Current.Response.Cookies.Add(langCookie);
                }
                catch (Exception) { }
            }
        }

    Test13Controller

        public class Test13Controller : CommonController
        {
                public ActionResult Index()
                {
                    return View();
                }
                public ActionResult ChangeLanguage(string lang)
                {
                    new LanguageMang().SetLanguage(lang);
                    return RedirectToAction("Index", "Test13");
                }
            
        }

    View

    @model WebApplication26.Models.test
    <ul>
        @foreach (var i in WebApplication26.Tool.LanguageMang.AvailableLanguages)
        {
            <li>@Html.ActionLink(i.LanguageFullName, "ChangeLanguage", "Test13", new { lang = i.LanguageCultureName }, null)</li>
        }
    </ul>
    @Html.DisplayNameFor(m => m.Name)

    Here is the result.

    Best Regards,

    YihuiSun

    Tuesday, August 11, 2020 7:36 AM