Open Closed

Get selected language in C# code #3909


User avatar
0
Naren created

HI, I am using ABP5.3 Blazor server app and I have language filter in the application for the user to select and I need the selected language to filter the dropdown based on the selected language I have some custom tables/code that needs list based on language filter. How do I get that in code to filter?


1 Answer(s)
  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Hi @Naren

    If you ask current language of current requrest, you can access CultureInfo.CurrentCulture

    If do you want to list of all languages of the application, you can inject ILanguageProvider and access localization options by calling GetLanguagesAsync() method on it.

    public class MyService : ITransientDependency
    {
    	protected ILanguageProvider LanguageProvider { get; }
    	public MyService(ILanguageProvider languageProvider)
    	{
    		LanguageProvider = languageProvider;
    	}
    
    	public async Task DoSomethingAsync()
    	{
    		var languages = await LanguageProvider.GetLanguagesAsync();
    		var currentLanguage = languages.FindByCulture(
    					CultureInfo.CurrentCulture.Name,
    					CultureInfo.CurrentUICulture.Name);
    
    		// ...
    	}
    }
    
Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09