Example #1
0
    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());
        }
    }
Example #2
0
        /// <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})");
                }
            }
        }