locked
Localizing DataProviders RRS feed

  • Question

  • i have created two DataProviders:

    * CertsProvider
    * CertsDeProvider

    Then i added both to the Startup.cs

    if (CultureInfo.CurrentCulture.Name == "de-DE")
    {
        svcs.AddScoped<CertsDeProvider>();
    }
    else
    {
        svcs.AddScoped<CertsProvider>();
    }

    and finally i added both to the cshtml:

    @if (CultureInfo.CurrentCulture.Name == "de-DE")
    {
        @inject WilderBlog.Services.DataProviders.CertsDeProvider certs
    }
    else
    {
        @inject WilderBlog.Services.DataProviders.CertsProvider certs
    }

    While the runtime i'm getting after accessing the site: InvalidOperationException: No service for type 'WilderBlog.Services.DataProviders.CertsProvider' has been registered.

    What can i do?


    Sincerly yours Sascha

    Thursday, November 5, 2020 6:21 PM

All replies

  • The DataProviders are looking so:

    namespace WilderBlog.Services.DataProviders
    {
        public class CertsProvider : DataProvider<Cert>
        {
            public CertsProvider(IHostEnvironment env) : base(env, "certificates.json")
            {
    
            }
    
            public override IEnumerable<Cert> Get()
            {
                return base.Get().OrderByDescending(p => p.Id).ToList();
            }
        }
    
        public class Cert
        {
            public int Id { get; set; }
            public string Certtitle { get; set; }
            public string Provider { get; set; }
            public string Years { get; set; }
            public string Content { get; set; }
            public string Link { get; set; }
        }
    }

    and 

    namespace WilderBlog.Services.DataProviders
    {
        public class CertsDeProvider : DataProvider<CertDe>
        {
            public CertsDeProvider(IHostEnvironment env) : base(env, "certificatesDe.json")
            {
            }
     
            public override IEnumerable<CertDe> Get()
            {
                return base.Get().OrderByDescending(=> p.Id).ToList();
            }
        }
     
        public class CertDe
        {
            public int Id { get; set; }
            public string Certtitle { get; set; }
            public string Provider { get; set; }
            public string Years { get; set; }
            public string Content { get; set; }
            public string Link { get; set; }
        }
    }


    Sincerly yours Sascha

    Thursday, November 5, 2020 6:24 PM
  • Your question appears to be related to build an ASP.NET application and not related to the C# language which is what this forum is for. For questions related to building ASP.NET applications please post in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, November 5, 2020 7:50 PM
  • Thanks. I opened a question there

    Sincerly yours Sascha

    Friday, November 6, 2020 7:14 AM
  • Hi,

    I saw the answer in asp.net forum and here is a suggestion that you can consider, in fact one provider should be more appropriate since you have the possibility to get the culture informations from the current thread culture ui infos, then you can have only one provider that performs culture check at the constructor level: 

          CultureInfo current = Thread.CurrentThread.CurrentUICulture;
    
          if(current.TwoLetterISOLanguageName == "de") {
             /* logic to assign the right resources or env variables*/
          } else if(current.TwoLetterISOLanguage == "en") {
            /* more logic to handle different cases /* 
          }

    hope it helps.

    Best regards.

     
    Friday, November 6, 2020 9:21 AM