-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
2,211 additions
and
1,924 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\DeepL.Service\DeepL.Service.csproj" /> | ||
<ProjectReference Include="..\..\src\DeepL\DeepL.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2022 DeepL SE (https://www.deepl.com) | ||
// Use of this source code is governed by an MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
using DeepL.Service; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace ASP.NET.Controllers; | ||
|
||
public class TranslateController : Controller { | ||
|
||
private readonly DeepLService deepl; | ||
|
||
public TranslateController(DeepLService deepLService) { | ||
this.deepl = deepLService; | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> Index(string translateMe, string targetLanguage) { | ||
var result = await deepl.client.TranslateTextAsync(translateMe, string.Empty, targetLanguage); | ||
return Ok(result.Text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2022 DeepL SE (https://www.deepl.com) | ||
// Use of this source code is governed by an MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
using DeepL.Model.Options; | ||
using DeepL.Service; | ||
|
||
namespace ASP.NET; | ||
|
||
public class Program { | ||
public static void Main(string[] args) { | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
|
||
// Add DeepL Services | ||
builder.Services.AddDeepL(options => { | ||
options.ServiceLifetime = ServiceLifetime.Singleton; | ||
options.ApiKey = "test"; | ||
options.TranslatorOptions = new TranslatorOptions() { | ||
appInfo = new AppInfo() { | ||
AppName = "test", | ||
AppVersion = "1.0.0", | ||
} | ||
}; | ||
}); | ||
|
||
|
||
|
||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
|
||
app.MapControllers(); | ||
|
||
app.Run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:41365", | ||
"sslPort": 44320 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "weatherforecast", | ||
"applicationUrl": "http://localhost:5030", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "weatherforecast", | ||
"applicationUrl": "https://localhost:7011;http://localhost:5030", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "weatherforecast", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DeepL\DeepL.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2022 DeepL SE (https://www.deepl.com) | ||
// Use of this source code is governed by an MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
using DeepL.Model.Interfaces; | ||
using DeepL.Model.Options; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace DeepL.Service; | ||
|
||
public class DeepLConfiguration { | ||
|
||
public TranslatorOptions TranslatorOptions { get; set; } | ||
public ServiceLifetime ServiceLifetime { get; set; } = ServiceLifetime.Scoped; | ||
public string ApiKey { get; set; } | ||
|
||
public Type? customTranslator { get; set; } = null; | ||
|
||
} | ||
|
||
public class DeepLService { | ||
|
||
public Translator client { get; set; } | ||
|
||
public DeepLService(IOptions<DeepLConfiguration> options) { | ||
if(options.Value == null) throw new ArgumentNullException(nameof(options.Value)); | ||
this.client = new Translator(options.Value.ApiKey, options.Value.TranslatorOptions); | ||
} | ||
} | ||
|
||
|
||
public static class DeepLExtension { | ||
|
||
/// <summary> | ||
/// Adds DeepL to DI | ||
/// </summary> | ||
/// <param name="services">Service Collection</param> | ||
/// <param name="lifetime">Service Lifetime</param> | ||
/// <param name="option" cref="TranslatorOptions">Translator Options</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddDeepL(this IServiceCollection services, Action<DeepLConfiguration> options) { | ||
|
||
if (services == null) throw new ArgumentNullException(nameof(services)); | ||
if (options == null) throw new ArgumentNullException(nameof(options)); | ||
|
||
//Apply Config | ||
services.AddOptions(); | ||
services.Configure(options); | ||
|
||
var deeplConfig = new DeepLConfiguration(); | ||
options?.Invoke(deeplConfig); | ||
|
||
// Lets see if we want to use a custom translator | ||
var translatorType = typeof(Translator); | ||
if (deeplConfig.customTranslator != null) { | ||
if (deeplConfig.customTranslator.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ITranslator))) { | ||
translatorType = typeof(Translator); | ||
} else { | ||
throw new ArgumentException("CustomTranslator must implement ITranslator Interface"); | ||
} | ||
} | ||
|
||
services.Add(new ServiceDescriptor(typeof(ITranslator), translatorType, deeplConfig.ServiceLifetime)); | ||
return services; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.