Table of Contents

Implementing your own custom source

If the sources from the TechTolk repository do not suffice for your project, you can easily implement your own sources. You do this by implementing the ITranslationSetSource interface with this method:

Task PopulateTranslationsAsync(ITranslationSetBuilder builder, SourceRegistrationBase sourceRegistration)

The ITranslationSetBuilder contains several methods to add translations for dividers.

You can use direct Add calls:

public class MyTranslationSetSource : ITranslationSetSource
{
    public Task PopulateTranslationsAsync(ITranslationSetBuilder builder, SourceRegistrationBase sourceRegistration)
    {
        var en = CultureInfoDivider.FromCulture("en-US");
        builder.Add(en, "MyKey", "MyTranslation");
        builder.Add(en, "AnotherKey", "AnotherTranslation");
        
        var nl = CultureInfoDivider.FromCulture("nl-NL");
        builder.Add(nl, "MyKey", "MijnVertaling");
        builder.Add(nl, "AnotherKey", "NogEenVertaling");

        return Task.CompletedTask;
    }
}

... or use the fluent API:

public class MyTranslationSetSource : ITranslationSetSource
{
    public Task PopulateTranslationsAsync(ITranslationSetBuilder builder, SourceRegistrationBase sourceRegistration)
    {
        builder
            .ForDivider(CultureInfoDivider.FromCulture("en-US")).Add(new[] {
                ( "MyKey", "MyTranslation"),
                ( "AnotherKey", "AnotherTranslation")
            })
            .ThenForDivider(CultureInfoDivider.FromCulture("nl-NL")).Add(new[] {
                ( "MyKey", "MijnVertaling"),
                ( "AnotherKey", "NogEenVertaling")
            });

        return Task.CompletedTask;
    }
}

Registering your custom source

The most simplest way to use your custom source is to instantiate it and pass it into the .FromSource(...) call.

.AddTranslationSet("SetName", set => {
    set.FromSource(new MyTranslationSetSource());
});

If your custom source needs services injected that can be resolved from the service provider, you can call .FromSource<MyTranslationSetSource>(). The source is resolved as soon as the translation set gets compiled.

.AddTranslationSet("SetName", set => {
    set.FromSource<MyTranslationSetSource>();
});

If your constructor is more complex, you must use a factory for your source by implementing the ITranslationSetSourceFactory<> interface, with the type of your source as type argument. You must register the factory with your service container yourself!

.AddTranslationSet("SetName", set => {
    set.FromSource<SourceWithComplexConstructor>();
})

// (...)

public class SourceWithComplexConstructor : ITranslationSetSource
{
    public SourceWithComplexConstructor(string connectionString) { /* ... */ }

    public Task PopulateTranslationsAsync(ITranslationSetBuilder builder, SourceRegistrationBase sourceRegistration)
    {
        /* ... */
    }
}

public class SourceWithComplexConstructorFactory : ITranslationSetSourceFactory<SourceWithComplexConstructor>
{
    // Don't forget to register this factory in the service container!

    public ITranslationSetSource Create(ResolveSourceRegistration sourceRegistration)
    {
        return new SourceWithComplexConstructor("MyConnectionString");
    }
}