turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 155 forks source link

How to get all translations for all languages for specific text? #378

Open tomasr78 opened 5 years ago

tomasr78 commented 5 years ago

I have several translations of the text, let's say [[[Sign-In]]]. I would like to iterate all translations and translate that text to MVC Route Table. Two problems here, I only find HttpContext.GetText method for in-code translation which requires HttpContext object which is not available in Routes method yet. Anyway I think I would have possibility to access Routes later in Controller context.

The main problem how to use HttpContext.GetText("[[[Sign-In]]]") and iterate all languages and get all translation available.

turquoiseowl commented 5 years ago

Off the top of my head you can use i18n.LanguageHelpers.GetAppLanguages() to reference collection of all languages then for each call i18n.Helpers.TextLocalizerExtensions.GetText. First argument would be LocalizedApplication.Current.TextLocalizerForApp.

tomasr78 commented 5 years ago

Is it possible to do that in-code translation without using HttpRequest object? The LocalizedApplication.Current.TextLocalizerForApp ask for it.

turquoiseowl commented 5 years ago

I don't think it does:

public ITextLocalizer TextLocalizerForApp { get { return RootServices.TextLocalizerForApp; } }
tomasr78 commented 5 years ago

The code below thrown exception "Request is not available in this context"}: System.Web.HttpException. Could you show how to use it properly in non HttpContext?

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
`LocalizedApplication.Current.TextLocalizerForApp.GetText("[[[Sign-In]]]", null, new[] { new LanguageItem(), }, out var langtag);`
turquoiseowl commented 5 years ago

Sorry, I don't have time at the moment. I suggest you build against the source to see what you can come up with.

turquoiseowl commented 5 years ago

Those suggestions above are untested BTW so not saying it should work.

MilanDanilovic commented 2 years ago

@tomasr78 It is possible. Vaadin framework example: import com.vaadin.flow.i18n.I18NProvider;

for(Locale locale : i18NProvider.getProvidedLocales()){
      String car = i18NProvider.getTranslation("vehicle.type.car",locale);
 }

Code above will take all available languages to get translation for passed value defined in its properties.