Open Closed

How can I set default language to FI #1315


User avatar
0
lan.dang created

currently, in my application, default language is EN, Now I want to set it to FI by default. How could I do it? BTW, how can I sync language bettwen API port 43308 to Angular UI port 4200?

Check the docs before asking a question: https://docs.abp.io/en/commercial/latest/ Check the samples, to see the basic tasks: https://docs.abp.io/en/commercial/latest/samples/index The exact solution to your question may have been answered before, please use the search on the homepage.

  • ABP Framework version: v4.3
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Steps to reproduce the issue:

18 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can set the default language in language mangement page.

  • User Avatar
    0
    christophe.baille created

    Helo,

    I am actually working with Lan.

    I changed as you said but it does not work, I changed it from host admin account.

    When I run the application and go to login page (Angular) it is still english, once login, it remain english.

  • User Avatar
    0
    christophe.baille created

    Is there anyone here to reply please? This is a big issue for us and after 1 week we do not have any answer or explanation...

    To be sure that the issue is not from changes from our project I created a new project, the default language is English. I then logged as host admin and put default language to Spanish. I then closed the application, restarted the project and open a browser in Incognito mode but still have the language in Spanish. Do ABP look at computer language or anything? We need to move in production

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi christophe.baille

    Sorry for the late. I will check it asap.

  • User Avatar
    0
    christophe.baille created

    Thanks. I nee some explanation about this "default language", here is what I did test:

    We have one solution in english and in Finnish, by default, whatever it is English or Finnish default, this happen:

    • Operation system in english, browser in english: ABP is in english
    • Operation system in Finnish, browser in english: ABP is in english
    • Operation system in Finnish, browser in Finnish: ABP is in Finnish

    So, if I understand, the ABP language is defined by: First, the browser language Second the operation system

    I then create a new solution, "disable" the english language, and put Spanish by default.But whn I run the solution, my application is in english, and I can not change th language. On this case it should not take the default language from ABP which is Spanish?

    First, I would like to understand on how th default language works. Second, I would like to know if there is any way to make a language by default, whatever is the OS or browser language.

    Thanks for your help

  • User Avatar
    1
    maliming created
    Support Team Fullstack Developer

    hi

    Abp will set the DefaultRequestCulture of RequestLocalizationOptions as your default language. Then the Localization middleware will handle localization.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-5.0#localization-middleware-2

    QueryStringRequestCultureProvider
    CookieRequestCultureProvider
    AcceptLanguageHeaderRequestCultureProvider
    

    put Spanish by default.But whn I run the solution, my application is in english, and I can not change th language. On this case it should not take the default language from ABP which is Spanish?

    Your browser request header may contains en. (accept-language: en,es;q=0.9,es;q=0.8)

    So will still use en, unless no language is found in QueryString, Cookie, AcceptLanguage, the default language will be used.

    You can consider removing AcceptLanguageHeaderRequestCultureProvider. The language will be stored in cookies when the user selects a language.

    Configure<RequestLocalizationOptions>(options =>
    {
        options.RequestCultureProviders.RemoveAll(x => x.GetType() == typeof(AcceptLanguageHeaderRequestCultureProvider));
    });
    
    

    hope this helps.: )

  • User Avatar
    0
    christophe.baille created

    I am not sure to understand your explanation about the last line of code, I think the language is already stored on cookies. If I open the app, select the language I want then close the browser, then open again, it will remember it and not take language from the OS or browser setting, so I guess it takes it from the cookie already.

    I just tried by adding lines of code you did mention in:

    ConfigureServices method from MyProjectApplicationModule.cs

    ConfigureLocalization method from MyProjectHttpApiHostModule.cs

    ConfigureLocalization method from MyProjectHttpApiModule.cs

    ConfigureServices method from MyProjectDomainModule.cs

    I changed in all project, just in case. But I did not see any difference on anything about the language management.

    What we need, if possible is:

    • when user open the app on his browser, whatever is the language of OS or browser, the first time it will be in Finnish
    • if the user change the language, then the next time he open again, it will be this language (not Finnish)

    My colleague did previously and put something similar to your code suggestion in startup.cs, it was taking the default languge at opening, but then we were not able to change the language anymore.

    Thanks for your help

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you try this? Please set your default language to Finnish,

    app.UseAbpRequestLocalization(x =>
    {
        x.RequestCultureProviders.RemoveAll(p => p.GetType() == typeof(AcceptLanguageHeaderRequestCultureProvider));
    });
    

    You need to restart the application to take effect when you change the default language.

    I will deal with this problem. https://github.com/abpframework/abp/issues/9158

  • User Avatar
    0
    christophe.baille created

    It is what my colleague did before, similar code, he added this into startup.cs but it was not running well.

            app.UseAbpRequestLocalization(options =>
            {
                var acceptLanguageHeaderRequestCultureProvider = options.RequestCultureProviders
                      .OfType&lt;AcceptLanguageHeaderRequestCultureProvider&gt;()
                      .FirstOrDefault();
                if (acceptLanguageHeaderRequestCultureProvider != null)
                {
                    options.RequestCultureProviders.Remove(acceptLanguageHeaderRequestCultureProvider);
                }                
            });
    

    I tried adding lines you mentionned on into MyProjectHttpApiHostModule.cs, OnApplicationInitialization method but it behave not correctly, the language selected is fine now (on the menu), but the text is in spanish and can not be changed:

    When I open the app from incognito, it will then open in Spanish (Default Language). If I open the app in another browser with English previously saved in cache/cookies for this application, it will open with the English language selected, but the text will be is Spanish. And in both cases, when I try to change the language from the menu, it will remain in Spanish...

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Can I check it remotely?

    https://zoom.us/j/98236731173?pwd=NGcvckhBUExCajhDRmg0UkkrSmxCdz09

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I will talk about this issue with my angular colleague in the afternoon.

    Angular is using accept-language instead of cookies.

    You can try the above code in mvc, it should works as your expected.

  • User Avatar
    0
    christophe.baille created

    Yes, I saw while you connected that mvc looks ok. I wait some news from you regarding Angular then, thank for your support

  • User Avatar
    0
    Mehmet created

    Hi,

    You can add an interceptor to get localizations in the language you specify. Please follow the steps below to achieve this:

    • Create a file called accept-language.interceptor.ts
    • Change its content with the following:
    import { SessionStateService } from '@abp/ng.core';
    import { HttpHandler, HttpHeaders, HttpInterceptor, HttpRequest } from '@angular/common/http';
    import { Injectable } from '@angular/core';
    
    @Injectable({ providedIn: 'root' })
    export class AcceptLanguageInterceptor implements HttpInterceptor {
      constructor(private sessionState: SessionStateService) {}
    
      intercept(request: HttpRequest<any>, next: HttpHandler) {
        return next.handle(
          request.clone({
            setHeaders: this.getAdditionalHeaders(request.headers),
          }),
        );
      }
    
      getAdditionalHeaders(existingHeaders?: HttpHeaders) {
        const headers = {} as any;
    
        // When sessionState.getLanguage() returns null at first initialization.
        // So you can set your default language as shown below:
        const lang = this.sessionState.getLanguage() || 'es' // put your default language here.
        if (!existingHeaders?.has('Accept-Language')) {
          headers['Accept-Language'] = lang;
        }
    
        return headers;
      }
    }
    
    • Add the provider below to the AppModule:
    providers: [
        //...
        {
          provide: HTTP_INTERCEPTORS,
          useExisting: AcceptLanguageInterceptor,
          multi: true,
        },
      ],
    

    I hope this help you.

  • User Avatar
    0
    christophe.baille created

    by adding this on angular, it is working now.

    However, when I go to https://localhost:44367/Account/Login it remain in english, then after login, I go back to Angular in Spanish. If I am right, it is related to this ticket: https://support.abp.io/QA/Questions/1324/How-to-change-language-of-login-page-when-user-changes-language-of-application

    Thanks

  • User Avatar
    0
    Mehmet created

    If I am right, it is related to this ticket

    Yes, you can follow the other ticket for the progress. I am closing this one. If you encounter any problem, feel free to open the ticket.

    Thanks.

  • User Avatar
    0
    christophe.baille created

    I still have the issue that if I go directly to this page https://localhost:44367/Account/Login, I will not have the language I want. I will see if it is part of our requirements. By the way, to make Angular working, I added the changes on Angular code but had to remove all the code suggested in .NET.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi christophe

    Since Angular can customize Accept-Language, you don't need to remove AcceptLanguageHeaderRequestCultureProvider in backend.

    https://github.com/abpframework/abp/pull/9162

  • User Avatar
    1
    christophe.baille created

    Hello maliming,

    You are right, I removed all changes mentionned for .net project before and just put changes on the angular part.

    So now when I run the angular page, I got the Finnish language and can change perfectly.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 19, 2024, 12:56