public async Task <List <string?>?> TranslateBatchAsync(List <string> list, string from, string to) { var apiKey = DeepLApiKey(); if (string.IsNullOrEmpty(apiKey)) { return(null); } using (DeepLClient client = new DeepLClient(apiKey)) { if (supportedLanguages == null) { supportedLanguages = (await client.GetSupportedLanguagesAsync()).ToList(); } if (!supportedLanguages.Any(a => a.LanguageCode == to.ToUpper())) { return(null); } var translation = await client.TranslateAsync(list, sourceLanguageCode : from.ToUpper(), targetLanguageCode : to.ToUpper()); return(translation.Select(a => (string?)a.Text).ToList()); } }
/// <summary> /// Gets the usage statistics and writes the to the console. /// </summary> /// <param name="authenticationKey">The authentication key for the DeepL API.</param> /// <param name="useFreeApi">Determines whether the free or the pro DeepL API is used.</param> private static async Task GetUsageStatisticsAsync(string authenticationKey, bool useFreeApi) { using (DeepLClient client = new DeepLClient(authenticationKey, useFreeApi)) { UsageStatistics usageStatistics = await client.GetUsageStatisticsAsync(); Console.WriteLine($"Currently billed characters: {usageStatistics.CharacterCount}"); Console.WriteLine($"Character limit: {usageStatistics.CharacterLimit}"); } }
/// <summary> /// Gets the supported languages and writes them to the console. /// </summary> /// <param name="authenticationKey">The authentication key for the DeepL API.</param> private static async Task GetSupportedLanguagesAsync(string authenticationKey, bool useFreeApi) { using (DeepLClient client = new DeepLClient(authenticationKey, useFreeApi)) { IEnumerable <SupportedLanguage> supportedLanguages = await client.GetSupportedLanguagesAsync(); foreach (SupportedLanguage supportedLanguage in supportedLanguages) { Console.WriteLine($"{supportedLanguage.Name} ({supportedLanguage.LanguageCode})"); } } }
/// <summary> /// Translates the specified text from the specified source language to the specified target language. /// </summary> /// <param name="authenticationKey">The authentication key for the DeepL API.</param> /// <param name="text">The text that is to be translated.</param> /// <param name="sourceLanguage">The language of the text.</param> /// <param name="targetLanguage">The language into which the text is to be converted.</param> private static async Task TranslateAsync(string authenticationKey, string text, string sourceLanguage, string targetLanguage) { using (DeepLClient client = new DeepLClient(authenticationKey)) { Translation translation = await client.TranslateAsync( text, sourceLanguage == null?null : Program.GetLanguageCode(sourceLanguage), Program.GetLanguageCode(targetLanguage) ); Console.WriteLine($"Detected source language: {translation.DetectedSourceLanguage}"); Console.WriteLine(); Console.WriteLine("Translation:"); Console.WriteLine(translation.Text); } }
/// <summary> /// Translates the specified document from the specified source language to the specified target language and saves the translated /// document into the specified file. /// </summary> /// <param name="authenticationKey">The authentication key for the DeepL API.</param> /// <param name="inputFile">The file that contains the document that is to be translated.</param> /// <param name="outputFile">The file into which the translated document is to be saved.</param> /// <param name="sourceLanguage">The language of the text.</param> /// <param name="targetLanguage">The language into which the text is to be converted.</param> private static async Task TranslateDocumentAsync( string authenticationKey, string inputFile, string outputFile, string sourceLanguage, string targetLanguage) { using (DeepLClient client = new DeepLClient(authenticationKey)) { await client.TranslateDocumentAsync( inputFile, outputFile, sourceLanguage == null?null : Program.GetLanguageCode(sourceLanguage), Program.GetLanguageCode(targetLanguage) ); } }
public StringDictionary(string dictPath) { string API_KEY_FILE = "../../../deepl_api_key.txt"; if (File.Exists(API_KEY_FILE)) { string apiKey = File.ReadAllText(API_KEY_FILE).Trim(); DeepLClient = new DeepLClient(apiKey); } this.dictPath = dictPath; saveDebouncer = new Debouncer(200 /* ms */, (Object src, ElapsedEventArgs e) => SaveDict()); Reload(); fileWatcher = new FileSystemWatcher(); fileWatcher.Path = Path.GetDirectoryName(dictPath); fileWatcher.Filter = Path.GetFileName(dictPath); fileWatcher.Changed += OnDictFileUpdated; fileWatcher.EnableRaisingEvents = true; }