Use Resources File(.resx) In Jquery Or Javascript In Asp.net Core RRS feed

  • Question

  • User338455301 posted


    i have website with 3 language 
    i must show message for any language like this : 

        if (lang == "en") {
                alert("update data successfully");
            else if (lang == "fa") {
                alert("اطلاعات با موفقیت ویرایش شد");
            else //fr 
                alert("Les informations ont été modifiées avec succès");

    but i want use resource file in jquery to manage my message  : 


          alert(@Localizor["Update Message"]);

    how to do it ? 

    Thursday, June 18, 2020 6:52 AM

All replies

  • User-821857111 posted

    Here's how to use Resource Files to for localisation in .NET Core: https://www.mikesdotnetting.com/article/346/using-resource-files-in-razor-pages-localisation

    Razor code won't work in JS files, so you should ensure that your jQuery code is included within the page/view.

    Monday, June 29, 2020 8:30 AM
  • User-2054057000 posted

    There can be many ways to do it. It depends on how you are implementing the Globalization & Localization in ASP.NET Core.

    First Way

    There is one way which is to get the culture from the query string in jQuery. This culture value will give you the current culture. So for example a person is viewing your website in english culture so the URL will be:


    Similarly for french it will be:


    So you can extract this culture query sting from jQuery using URLSearchParams.

    const urlParams = new URLSearchParams(window.location.search);
    const myParam = urlParams.get('culture');

     Next you can use the if else block to show alert:

    if (lang == "en") {
        alert("update data successfully");
    else if (lang == "fa") {
        alert("اطلاعات با موفقیت ویرایش شد");
    else if (lang == "fr") 
        alert("Les informations ont été modifiées avec succès");

    Second Way

    There can be another way to use jQuery and call a controller using AJAX. In this controller you return the current culture. Once you get the culture value you can then apply if else block.

    I recommend you to first go through how Globalization & Localization works in ASP.NET Core then it become easy to create your logic - How to use Globalization and localization in ASP.NET Core

    Enjoy coding !

    Monday, June 29, 2020 9:44 AM
  • User1321565059 posted


    you can access your localizer in jquery . just use it in '' .

    var successForm = '@_Localizer["SuccessForm"]';

    and as you know ,  in razor page you should inject your localizer


    @using Microsoft.Extensions.Localization 
    @inject IStringLocalizer<MySharedTranslate> _Localizer;


    Saturday, September 12, 2020 10:01 PM
  • User585649674 posted

    in _layout.cshtml add a json like below

    var localizedData = {

    Then inside any of the jquery calls or javascript calls. You can use like below

    function CallApi(){

    Please note above method will work only when the whole page is refreshed and _layout is reloaded when the language is changed by the user

    Thursday, September 24, 2020 9:34 AM