Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service collection extensions #28

Closed
wants to merge 1 commit into from
Closed

Add service collection extensions #28

wants to merge 1 commit into from

Conversation

antoniovalentini
Copy link

Description

I had some time to play around with the deepl-client (you did an awesome job by the way) and I thought it may be useful to add a set of ServiceCollection extensions to streamline Translator usage combined with the .NET built-in dependency injection pipeline (probably related to the #26 issue).

In this PR I wrote a small POC, and here's how to use it:

configuration

// create a new service collection or use the .NET built-in one
var services = new ServiceCollection();

// load configuration from JSON files or environment variables
var config = LoadConfiguration();

// use the new `AddDeepL`
services.AddDeepL(config, options =>
{
    // and override the options based on your preferences
    options.appInfo = new AppInfo
    {
        AppName = "test-app",
        AppVersion = "0.0.1",
    };
});

actual usage

public class MyClass
{
    private readonly ITranslator _translator;

    public Impl(ITranslator translator)
    {
        _translator = translator;
    }

    public async Task<string> Translate()
    {
        const string sourceText = "Hello world!";
        return await _translator.TranslateTextAsync(sourceText, LanguageCode.English, LanguageCode.Italian);
    }
}

@antoniovalentini
Copy link
Author

It's being included in the development branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant