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>
        /// 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);
            }
        }
Example #3
0
        private async Task CloudTranslateAsync()
        {
            // Go through our dictionary looking for potential strings to translate.
            var strings = new List <string>();

            foreach (var jp in translations.Keys)
            {
                if (ShouldTranslateString(jp) && translations[jp] == null)
                {
                    strings.Add(jp);
                }
            }
            if (strings.Count == 0)
            {
                return;
            }
            var result = (await DeepLClient.TranslateAsync(strings, Language.Japanese, Language.English, Splitting.None, true)).ToList();

            for (var i = 0; i < strings.Count; i++)
            {
                translations[strings[i]] = result[i].Text;
            }
            SaveDict();
        }