D
D
Dmitry Filandor2015-07-14 12:19:14
JavaScript
Dmitry Filandor, 2015-07-14 12:19:14

How to localize asp mvc application based on views?

Hello! It is necessary to make a bilingual site. Just for views, no resources needed.
I do everything according to the article blogs.msdn.com/b/vyunev/archive/2011/12/07/asp-net...
with grief in half started it, reading from cookies was not involved in the base controller, added:

string cultureName = null;

            // Attempt to read the culture cookie from Request
            HttpCookie cultureCookie = Request.Cookies["_culture"];
            if (cultureCookie != null)
                cultureName = cultureCookie.Value;
            else
                cultureName = Thread.CurrentThread.CurrentCulture.Name;

But I can’t do the display of the current language in any way, in the view of Index.ru-ru.cshtml and Index.en-us.cshtml
@{

    var culture = System.Threading.Thread.CurrentThread.CurrentUICulture.Name.ToLowerInvariant();
}

@helper selected(string c, string culture)
{
    if (c == culture)
    {
        @:checked="checked"
            }
}

@using (Html.BeginForm("SetCulture", "Home"))
{
    <fieldset>          
        <input name="culture" id="ru-ru" value="ru-ru" type="radio" @selected("ru-ru", culture) /> Русский <br />                 
        <input name="culture" id="en-us" value="en-us" type="radio" @selected("en-us", culture) /> English
    </fieldset>
}
...


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">
        (function ($) {
            $("input[type = 'radio']").click(function () {
                $(this).parents("form").submit(); // post form
            });

        })(jQuery);
    </script>
}

I always have Russian radio selected, no matter what language is selected. And this is if (c I don’t understand what the variable is, where it comes from. It’s
still not clear why it has two identical blocks of js code and why make the selected language bold, anyway the view is overloaded and the style is reset

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2015-07-14
@dmitryKovalskiy

The author of the article refers to the fact that some languages ​​have a different direction of writing from Russian, and the use of resource files will not give the desired effect. In your case - what is the reason for using different Views? Fundamentally great content ? I also personally dislike filling the controller with locale extraction logic. Make, for example, an AJAX request to a separate method by setting this value. And reading recommendations from 4 years ago is probably not the best idea.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question