"Knowledge has to be improved, challenged, and increased constantly, or it vanishes."

Taking Your ASP.NET Core 7 Localization to the Next Level: Localizing Layout

This post is a continuation of my previous post, Globalization & Localization in ASP.Net Core 7,  (https://weblogs.asp.net/sreejukg/globalization-localization-in-asp-net-core-7), where I explained the importance of Globalization and Localization and how to apply them to your Razor Pages. However, the previous post only covered localizing Razor Pages and did not discuss localizing the layout files of an ASP.Net Core Razor Pages application. In this post, we will be exploring how to localize the text in the layout files, so I encourage you to read the previous post to gain a better understanding of the localization topics covered in this post.

In this post, we'll continue our discussion on Globalization and Localization in ASP.Net Core 7, specifically on localizing text in the layout files of a Razor Pages application. As a quick recap, in Part 1 of this series, we covered how to localize Razor Pages using Model classes. See the below Image that was the output of our localized application.

clip_image002

However, as shown in the above image, the Header and Footer of the app are loaded from the layout file and are not localized. So, in this article, we'll focus on localizing the contents inside the layout page.

Note:- We will be working in the same example project that we created for the previous article. You may find the GitHub URL to the source code at the end of this post.

Localize Contents inside Layout file

To localize the text in layout files, as the layout file is not supported by a Model class, we need to have an alternative approach. We can create an empty class that represents the localized content and define properties that correspond to the different pieces of text that need to be localized. We can then create a resource file with the same name as the class and use it to localize the data in the layout page.

So first, let me create a class to the root of the application, and call it “SharedResource.cs”. See the class definition below.

clip_image004

Since the purpose of this class is just to load the resource file, no properties are needed for this. So let us leave it empty.

Now let me add a resource file under the resources folder with name SharedResource.resx. If you create the placeholder class inside a subfolder, the resource file to be named accordingly. Recollect, we named our resource file for IndexModel class (in the previous post) as Pages.IndexModel.resx. The Resource file need to be fully qualified relative to the assembly.

clip_image006

To localize text in the application, you can add keys and corresponding values to the resource file. In this case, only three keys need to be localized, and updated them as follows. You need to make sure the Access Modifier selected to public.

clip_image008

Now let me add SharedResource.ar.resx and SharedResource.fr.resx as below.

clip_image010

clip_image012

Localize the Layout

To access the IStringLocalizer<T> and IHtmlLocalizer<T> instances in the layout file, you can use dependency injection. The DI framework can load the corresponding resources based on the model class that is passed.

In this case, since the HTML text is not stored in the Resource file, you can use the IStringLocalizer<T> instance. You can pass any model class and the DI framework will load the corresponding resources.

In the _Layout.cshtml file, let me inject the instances of IStringLocalizer as below.


@inject IStringLocalizer<SharedResource> sharedLocalizer;

As we did in Part 1, we can use the data defined in the resource file to localize text in our application. By accessing the appropriate localizer instance, we can retrieve the localized text values and use them to populate our application's UI. See the below statement that loads the data for the key “ApplicationTitle” from the resource file.


<title>@ViewData["Title"] - @sharedLocalizer["ApplicationTitle"]</title>

See the complete Layout page below.

clip_image014

Now let me run the Arabic page by calling the LoadArabic by calling the LoadArabic Handler

clip_image016

You may find the source code for this post from the below link. https://github.com/sreejukg/Localization/tree/LocalizationPart2/LocalizationSample/LocalizationSample

Summary

In the previous article, we learned how to apply localization to ASP.NET Core Razor pages. In this article, we focused on localizing layout files, which are not supported by Model classes.

8 Comments

  • Would have been a nice opportunity to show the RTL (right to left) layout for appropriate cultures.
    The challenge of i18n/Internationalisation i find to be the pluralisations, and that if you try to generalise you run afoul of genders/plurals and layout spacing. The advice i last read to ensure spacing in a product quick view with sale pricing for example was to have it as an image and one per culture. The layout should generally accept much shorter and longer phrasing depending on language, and the entire phrase with prefix and suffix (and unit and value and plurals etc should be defined for translation - similar to the unicode page https://unicode-org.github.io/icu/userguide/format_parse/messages/ ) so layout spacing is hard to define cleanly without images.

  • <a href=" www.vcaretechs.in /" rel="dofollow">Payment gateway integration </a> refers to the process of connecting a website or application with a payment gateway service. This allows users to securely make online transactions, such as purchasing goods or services, using their credit or debit cards.

    The process of integrating a payment gateway typically involves the following steps:

    1. Selecting a payment gateway provider: This includes researching and comparing different providers based on their fees, security features, and supported payment methods.

    2. Signing up for an account: Once you have selected a payment gateway provider, you will need to sign up for an account to access their integration documentation and support.

    3. Obtaining an API key: Your payment gateway provider will give you an API key that you will use to connect your website or application with their service.

    4.Implementing the integration: The payment gateway provider will give you integration instructions, typically in the form of code snippets, that you will need to add to your website or application to complete the integration.

    5.Testing the integration: Once you have implemented the integration, you should test it to ensure that it is working correctly and that all transactions are being processed securely.

    It's important to note that different payment gateway providers may have different integration requirements, so be sure to carefully read and follow their documentation. Additionally, some providers may require additional steps such as compliance check and PCI DSS certification to be done.



  • <a href=" www.vcaretechs.in /" rel="dofollow">Payment gateway integration </a> refers to the process of connecting a website or application with a payment gateway service. This allows users to securely make online transactions, such as purchasing goods or services, using their credit or debit cards.

    The process of integrating a payment gateway typically involves the following steps:

    1. Selecting a payment gateway provider: This includes researching and comparing different providers based on their fees, security features, and supported payment methods.

    2. Signing up for an account: Once you have selected a payment gateway provider, you will need to sign up for an account to access their integration documentation and support.

    3. Obtaining an API key: Your payment gateway provider will give you an API key that you will use to connect your website or application with their service.

    4.Implementing the integration: The payment gateway provider will give you integration instructions, typically in the form of code snippets, that you will need to add to your website or application to complete the integration.

    5.Testing the integration: Once you have implemented the integration, you should test it to ensure that it is working correctly and that all transactions are being processed securely.

    It's important to note that different payment gateway providers may have different integration requirements, so be sure to carefully read and follow their documentation. Additionally, some providers may require additional steps such as compliance check and PCI DSS certification to be done.



  • <a href=" www.vcaretechs.in /" rel="dofollow">Payment gateway integration </a> refers to the process of connecting a website or application with a payment gateway service. This allows users to securely make online transactions, such as purchasing goods or services, using their credit or debit cards.

    The process of integrating a payment gateway typically involves the following steps:

    1. Selecting a payment gateway provider: This includes researching and comparing different providers based on their fees, security features, and supported payment methods.

    2. Signing up for an account: Once you have selected a payment gateway provider, you will need to sign up for an account to access their integration documentation and support.

    3. Obtaining an API key: Your payment gateway provider will give you an API key that you will use to connect your website or application with their service.

    4.Implementing the integration: The payment gateway provider will give you integration instructions, typically in the form of code snippets, that you will need to add to your website or application to complete the integration.

    5.Testing the integration: Once you have implemented the integration, you should test it to ensure that it is working correctly and that all transactions are being processed securely.

    It's important to note that different payment gateway providers may have different integration requirements, so be sure to carefully read and follow their documentation. Additionally, some providers may require additional steps such as compliance check and PCI DSS certification to be done.



  • <a href=" www.vcaretechs.in /" rel="dofollow">Payment gateway integration </a> refers to the process of connecting a website or application with a payment gateway service. This allows users to securely make online transactions, such as purchasing goods or services, using their credit or debit cards.

    The process of integrating a payment gateway typically involves the following steps:

    1. Selecting a payment gateway provider: This includes researching and comparing different providers based on their fees, security features, and supported payment methods.

    2. Signing up for an account: Once you have selected a payment gateway provider, you will need to sign up for an account to access their integration documentation and support.

    3. Obtaining an API key: Your payment gateway provider will give you an API key that you will use to connect your website or application with their service.

    4.Implementing the integration: The payment gateway provider will give you integration instructions, typically in the form of code snippets, that you will need to add to your website or application to complete the integration.

    5.Testing the integration: Once you have implemented the integration, you should test it to ensure that it is working correctly and that all transactions are being processed securely.

    It's important to note that different payment gateway providers may have different integration requirements, so be sure to carefully read and follow their documentation. Additionally, some providers may require additional steps such as compliance check and PCI DSS certification to be done.

  • <a href=" www.vcaretechs.in /" rel="dofollow">Payment gateway integration </a> refers to the process of connecting a website or application with a payment gateway service. This allows users to securely make online transactions, such as purchasing goods or services, using their credit or debit cards.

    The process of integrating a payment gateway typically involves the following steps:

    1. Selecting a payment gateway provider: This includes researching and comparing different providers based on their fees, security features, and supported payment methods.

    2. Signing up for an account: Once you have selected a payment gateway provider, you will need to sign up for an account to access their integration documentation and support.

    3. Obtaining an API key: Your payment gateway provider will give you an API key that you will use to connect your website or application with their service.

    4.Implementing the integration: The payment gateway provider will give you integration instructions, typically in the form of code snippets, that you will need to add to your website or application to complete the integration.

    5.Testing the integration: Once you have implemented the integration, you should test it to ensure that it is working correctly and that all transactions are being processed securely.

    It's important to note that different payment gateway providers may have different integration requirements, so be sure to carefully read and follow their documentation. Additionally, some providers may require additional steps such as compliance check and PCI DSS certification to be done.



  • I couldn't contain my excitement when I came across a website that offers a wide range of vector images and stock photos. From abstract botanical art pieces that ignite my creativity to stunning snapshots of nature's beauty, this platform satisfies my visual cravings like never before.

  • Our Microsoft AZ-900 practice test is the most reliable solution to quickly prepare for your Microsoft Designing Microsoft Azure Infrastructure Solutions. We are certain that our Microsoft AZ-900 practice exam will guide you to get certified on the first try. Here is how we serve you to prepare successfully:

Add a Comment

As it will appear on the website

Not displayed

Your website