Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

How to enable database localization in AspNetZero template #5046


User avatar
0
webtalents created

I want to use database as a source for localization's texts. What would be the steps to achieve this using AspNetZero template?


5 Answer(s)
  • User Avatar
    0
    hitaspdotnet created

    Hi, Step 1. See [https://aspnetboilerplate.com/Pages/Documents/Zero/Language-Management#enabledblocalization])

    Step 2. Enjoy the most professional .Net template

    NOTE: Database localization feature is enabled by default in AspNetZero template. [https://github.com/aspnetzero/aspnet-zero-core/blob/dd37d3838d0975d97a2d5abb85ae40a57e4616b8/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Web.Core/AbpZeroTemplateWebCoreModule.cs#L60])

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks @csbeginner :).

    @webtalents, you can go to Languages page, then select "Actions > Change texts" for a language and modify localization on the UI. But, you cannot define localization keys on the UI.

  • User Avatar
    0
    webtalents created

    Hi @csbeginner and @ismcagdas. Thank you for your answers. I had read already the manual (before posted question here) and checked the template which @csbeginner mentioned in the answer. I was also able to add new keys by editing XML files. The template I use is of 5.3 version and it consists of

    Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
    

    in

    PreInitialize()
    

    My questions are:

    What are the steps I need to do to get the the data in AbpLanguagesTexts table? Is there internal method in Zero module to get the data from XML and insert it to AbpLanguagesTexts table ?
    Can I simply delete the XML files and insert the data to AbpLanguagesTexts table using SQL query? Will the language management work with DB as a source for languages texts in the way like it works with XML files?

  • User Avatar
    0
    hitaspdotnet created

    If your question is related to the base fields in YourProject.xml/json you need to implement a method to read xml/json and write it on the database. The fact that you can include a variety of sources(xml/json/system file/resx) in your code is one of the unique features of this framework. As a customer, we can not expect the development team to include 750 translation fields in database seed when that xml/json source solve users need's.

    So, you can use ApplicationTextRepository and ApplicationTextManager to add your own locale keys or xml resources to Db.

    public async Task ImportResourcesFromXml(ApplicationLanguage language, string xml, bool updateExistingResources = true)
    {
        var xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);
        var nodes = xmlDoc.SelectNodes(@"AddressToYourXml");
    
        var existingResources = _appTxtRepo.GetAll().Where(txt => txt.LanguageName == language.Name);
    
        foreach (XmlNode node in nodes)
        {
            var name = node.Attributes["name"].InnerText.Trim();
            var value = "";
            var valueNode = node.SelectSingleNode("value");
            if (valueNode != null)
                value = valueNode.InnerText;
    
            if (string.IsNullOrEmpty(name))
                continue;
    
            var resource = existingResources.FirstOrDefault(x => x.Key.Equals(name, StringComparison.InvariantCultureIgnoreCase));
            if (resource != null)
            {
                if (updateExistingResources)
                {
                    resource.Value = value;
                }
            }
            else
            {
                await _appTxtManager.UpdateStringAsync(language.Name, CultureInfoHelper.Get(language.Name), name, value );
            }
        }
    }
    

    CODE NOT COMPILED It's just for sample. If you need help for read/write xml resource please leave comment here I can help you with simple code.

  • User Avatar
    0
    ismcagdas created
    Support Team

    @webtalents, you must not delete xml files, because localization keys are retrieved from xml files. Database contains localized values not the localization keys.

    See <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Zero/Language-Management#note-on-existing-xml-localization-sources">https://aspnetboilerplate.com/Pages/Doc ... on-sources</a>.