Base solution for your next web application
Open Closed

MAUI Blazor: Localization Not Updating When Modified Locally #12498


User avatar
0
JeffMH created

Values in the .xml file for localization are not reliably appearing in the Blazor views. For example, we have two values in the XML:

<text name="FreePlan">Free Plan</text> <text name="UpgradePlan">Upgrade Plan</text>

FreePlan resolved to Free Plan properly. However, UpgradePlan always returns as the key name "UpgradePlan" instead of the value "Upgrade Plan". I've stepped through your code, and the value isn't even in the dictionary. It doesn't matter where I put the UpgradePlan in the file - it is ignored. We have similar issues with other keys. There are no duplicate keys.

Example razor:

<div>

<span class="btn bg-keyLime me-3 fs-hg-sl-b13 fw-medium text-gray-900 border border-keyLime-active border-dashed px-4">@L("FreePlan")</span>

<NavLink href="/GetFullAccess" class="btn btn-primary fw-medium fs-hg-sl-b13 px-4" role="button">@L("UpgradePlan") <i class="icon-left-arrow float-end text-white ms-3"></i></NavLink>

</div>

I've verified the XML is correct. The code is correct as well:

Even more strange, if I change key "FreePlan"'s text value to "Free Plan 123", it still shows "Free Plan" in the app. Even after an uninstall, clean, bin/obj wipe, and so forth.

WHERE are the values coming from, please? Why are values seemingly not being read from the local XML?

This is blocking releasing an app because we can't localize.

Thanks for any help you can provide.

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

2 Answer(s)
  • User Avatar
    0
    JeffMH created

    The more I think about it - is all of this information stored on the ABP web/API?

    If so, which I'm guessing it may be, can you make it local? Requiring a new web/API deployment for every app release seems unnecessary.

    Thanks!

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @JeffMH

    ASP.NET Zero uses a database driven approach to manage localization texts. This allows you to change texts while the application is running without needing to recompile or redeploy.

    1. Initial Seeding: The .xml files in your solution's .Core project act as a source only when the application runs for the first time or when a new key is introduced. The system reads the keys from the XML and inserts them into the AbpLanguageTexts table in the database.

    2. The Database Takes Precedence: Once a key (like FreePlan) has been written to the database, the application no longer consults the XML file for that key. The value is always read directly from the database. This explains why changing the value of FreePlan to "Free Plan 123" in your XML file had no effect. The application continued to use the existing value from the database.

    3. How the Blazor Client Gets the Data: When the Blazor application starts, it makes a single API call to the backend to fetch all the necessary localization texts for the current user's language. These texts are then cached in memory on the Blazor client side. When you call L("UpgradePlan"), it looks for the key in this in memory cache. Since the UpgradePlan key was never added to the database, it's not present in the cache, and the method returns the key itself as a fallback.

    <br/> <br/> How to Fix the Issue and the Correct Workflow

    The correct way to add a new localization key is to add it to the relevant XML file in your backend solution. This file is typically located in the Localization/MyProjectName folder of your YourProjectName.Core project.

    Example: MyProjectName.xml

    <text name="FreePlan">Free Plan</text>
    <text name="UpgradePlan">Upgrade Plan</text>
    

    Update the Database

    After adding the new key to the XML file, you must get it into the database.

    • Run your backend project. When the application starts, ABP's seeding mechanism will scan the XML files and add any new keys that are not already in the AbpLanguageTexts table.

    Edit Texts from the Admin UI

    Now that your new key is in the database, you should use the application's admin interface not the XML file to manage its value or add translations for other languages.

    1. Log in to your application as an admin user.
    2. Navigate to Administration > Languages.
    3. Click the Change Texts or Details button next to the language you want to edit.
    4. In the list, find your newly added key, UpgradePlan. Its value will likely be empty. Enter the desired text "Upgrade Plan" here and save.
    5. If you want to change the value of an existing key, you must also do it from this interface.

    When you restart or refresh your Blazor application, it will fetch the updated values from the API, and L("UpgradePlan") will correctly resolve to "Upgrade Plan". <br/> <br/> The Advantage of Centralized Localization

    Your comment, "Requiring a new web/API deployment for every app release seems unnecessary," is a very valid point. However, there is a powerful reason why ASP.NET Zero chose this architecture: Agility.

    With this centralized system:

    • You do not need to recompile and resubmit your Blazor, Angular, or mobile apps to the app stores just to fix a typo in the UI text.
    • You simply update the text in the admin panel, and all clients will fetch the correct text the next time they start.
    • A non technical person, like a translator or a product manager, can update texts without ever touching the code or dealing with deployments.

    This provides a significant administrative advantage, especially for multi language and multi platform applications. Therefore, embracing this intended workflow is generally more efficient in the long run.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)