aksoftware98 / multilanguages

AKSoftware.Localization.MultiLanguages is a package for .NET developers allows them to easily build apps target multiple languages with just few lines of code
MIT License
85 stars 15 forks source link

Dynamically List Available languages : Selution #54

Closed gheith3 closed 4 weeks ago

gheith3 commented 1 year ago

create AddLanguageContainerSupportService.cs inside your project then replase every thing inside the class with next code

using System.Globalization;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;

namespace AKSoftware.Localization.MultiLanguages;

public static class AddLanguageContainerSupportExtensions
{
    public static IServiceCollection AddAddLanguageContainerSupport(
        this IServiceCollection services,
        Assembly assembly,
        string folderName = "Resources")
    {
       return services.AddSingleton<IAddLanguageContainerSupportService, AddLanguageContainerSupportService>(s => new AddLanguageContainerSupportService(assembly, folderName));
    }
}

public interface IAddLanguageContainerSupportService
{
    List<CultureInfo> GetSupportedCultures();
}

public class AddLanguageContainerSupportService : IAddLanguageContainerSupportService
{
    private readonly Assembly _assembly;
    private readonly string _folderName;

    public AddLanguageContainerSupportService(Assembly assembly, string folderName)
    {
        _assembly = assembly;
        _folderName = folderName;
    }

    public List<CultureInfo> GetSupportedCultures()
    {
        var languagesYamlFileNames = GetLanguagesYamlFileNames();
        if (languagesYamlFileNames == null || !languagesYamlFileNames.Any())
        {
            return new List<CultureInfo>();
        }

        var cultures = languagesYamlFileNames
            .Select(s => s.Split('.')[^2])
            .Select(s => new CultureInfo(s))
            .ToList();
        return cultures;
    }

    private List<string>? GetLanguagesYamlFileNames()
    {
        try
        {
            return _assembly
                .GetManifestResourceNames()
                .Where(s =>
                    s.Contains(_folderName) &&
                    (s.Contains(".yml") ||
                     s.Contains(".yaml")) &&
                    s.Contains("-"))
                .ToList();
        }
        catch (Exception ex)
        {
            // Handle the error as needed
            Console.WriteLine($"Error: {ex.Message}");
        }
        return new List<string>();
    }
}

in Program.cs file after

builder.Services.AddLanguageContainerForBlazorServer<EmbeddedResourceKeysProvider>(Assembly.GetExecutingAssembly(), "Resources");

add

builder.Services.AddAddLanguageContainerSupport(Assembly.GetExecutingAssembly());

then in your Combonent add

...
@inject IAddLanguageContainerSupportService LanguageContainerSupport

@foreach (var culture in LanguageContainerSupport.GetSupportedCultures())
          {
              <img src="@($"https://flagsapi.com/{culture.Name.Split("-")[1].ToUpper()}/shiny/64.png")"
                   @onclick='() => SetLanguage(culture.Name)' style="width:75px;cursor:pointer;margin:10px"/>
          }

...

by call LanguageContainerSupport.GetSupportedCultures() you will get List<CultureInfo> of all available Languages

GregFinzer commented 4 weeks ago

This will be in version 6.0. It is solved by this pull request: https://github.com/aksoftware98/multilanguages/pull/61