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 method to get translation language names #1148

Merged
merged 1 commit into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
<ComboBox x:Name="Languages"
Margin="0,10,0,0"
HorizontalAlignment="Stretch"
DisplayMemberPath="Value"
DisplayMemberPath="Name"
Header="Target Language:"
SelectedValuePath="Key" />
SelectedValuePath="Code" />
<Button x:Name="Translate"
Margin="0,10,0,0"
Click="Translate_OnClick"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,10 @@ private async void GetLanguages_OnClick(object sender, RoutedEventArgs e)
Languages.ItemsSource = null;

_translatorClient.SubscriptionKey = TranslatorServiceKey.Text;
var languages = await _translatorClient.GetLanguagesAsync();
var languages = await _translatorClient.GetLanguageNamesAsync();

var dictionary = new Dictionary<string, string>();
languages.ToList().ForEach(lang =>
{
try
{
var culture = new CultureInfo(lang);
if (!culture.EnglishName.StartsWith("Unknown"))
{
dictionary.Add(lang, culture.EnglishName);
}
}
catch
{
}
});

Languages.ItemsSource = dictionary.OrderBy(d => d.Key);
Languages.SelectedValue = dictionary.Keys.FirstOrDefault();
Languages.ItemsSource = languages.OrderBy(d => d.Name);
Languages.SelectedIndex = 0;

Shell.Current.DisplayWaitRing = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="Core\DataProviderBase{TConfig,TSchema}.cs" />
<Compile Include="Core\ExtensionMethods.cs" />
<Compile Include="Core\IDataService{T,U,V}.cs" />
<Compile Include="Services\MicrosoftTranslator\ServiceLanguage.cs" />
<Compile Include="Services\Twitter\TwitterStreamCallbacks.cs" />
<Compile Include="Services\Bing\BingLanguage.cs" />
<Compile Include="Services\Bing\BingCountry.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public interface ITranslatorService
/// <seealso cref="GetLanguagesAsync"/>
string Language { get; set; }

/// <summary>
/// Initializes the <see cref="TranslatorService"/> class by getting an access token for the service.
/// </summary>
/// <returns>A <see cref="Task"/> that represents the initialize operation.</returns>
/// <exception cref="ArgumentNullException">The <see cref="SubscriptionKey"/> property hasn't been set.</exception>
/// <exception cref="TranslatorServiceException">The provided <see cref="SubscriptionKey"/> isn't valid or has expired.</exception>
/// <remarks>Calling this method isn't mandatory, because the token is get/refreshed everytime is needed. However, it is called at startup, it can speed-up subsequest requests.</remarks>
Task InitializeAsync();

/// <summary>
/// Detects the language of a text.
/// </summary>
Expand All @@ -50,7 +59,7 @@ public interface ITranslatorService
/// </exception>
/// <exception cref="TranslatorServiceException">The provided <see cref="SubscriptionKey"/> isn't valid or has expired.</exception>
/// <remarks><para>This method performs a non-blocking request for language detection.</para>
/// <para>For more information, go to http://msdn.microsoft.com/en-us/library/ff512427.aspx.
/// <para>For more information, go to https://docs.microsofttranslator.com/text-translate.html#!/default/get_Detect.
/// </para></remarks>
/// <seealso cref="GetLanguagesAsync"/>
/// <seealso cref="Language"/>
Expand All @@ -63,19 +72,24 @@ public interface ITranslatorService
/// <exception cref="ArgumentNullException">The <see cref="SubscriptionKey"/> property hasn't been set.</exception>
/// <exception cref="TranslatorServiceException">The provided <see cref="SubscriptionKey"/> isn't valid or has expired.</exception>
/// <remarks><para>This method performs a non-blocking request for language codes.</para>
/// <para>For more information, go to http://msdn.microsoft.com/en-us/library/ff512415.aspx.
/// <para>For more information, go to https://docs.microsofttranslator.com/text-translate.html#!/default/get_GetLanguagesForTranslate.
/// </para>
/// </remarks>
Task<IEnumerable<string>> GetLanguagesAsync();

/// <summary>
/// Initializes the <see cref="TranslatorService"/> class by getting an access token for the service.
/// Retrieves friendly names for the languages available for text translation.
/// </summary>
/// <returns>A <see cref="Task"/> that represents the initialize operation.</returns>
/// <param name="language">The language used to localize the language names. If the parameter is set to <strong>null</strong>, the language specified in the <seealso cref="Language"/> property will be used.</param>
/// <returns>An array of <see cref="ServiceLanguage"/> containing the language codes and names supported for translation by <strong>Microsoft Translator Service</strong>.</returns>
/// <exception cref="ArgumentNullException">The <see cref="SubscriptionKey"/> property hasn't been set.</exception>
/// <exception cref="TranslatorServiceException">The provided <see cref="SubscriptionKey"/> isn't valid or has expired.</exception>
/// <remarks>Calling this method isn't mandatory, because the token is get/refreshed everytime is needed. However, it is called at startup, it can speed-up subsequest requests.</remarks>
Task InitializeAsync();
/// <remarks><para>This method performs a non-blocking request for language name.</para>
/// <para>For more information, go to https://docs.microsofttranslator.com/text-translate.html#!/default/post_GetLanguageNames.
/// </para>
/// </remarks>
/// <see cref="GetLanguagesAsync"/>
Task<IEnumerable<ServiceLanguage>> GetLanguageNamesAsync(string language = null);

/// <summary>
/// Translates a text string into the specified language.
Expand All @@ -93,7 +107,7 @@ public interface ITranslatorService
/// <exception cref="ArgumentException">The <paramref name="text"/> parameter is longer than 1000 characters.</exception>
/// <exception cref="TranslatorServiceException">The provided <see cref="SubscriptionKey"/> isn't valid or has expired.</exception>
/// <remarks><para>This method perform a non-blocking request for text translation.</para>
/// <para>For more information, go to http://msdn.microsoft.com/en-us/library/ff512421.aspx.
/// <para>For more information, go to https://docs.microsofttranslator.com/text-translate.html#!/default/get_Translate.
/// </para>
/// </remarks>
/// <seealso cref="Language"/>
Expand All @@ -114,7 +128,7 @@ public interface ITranslatorService
/// <exception cref="ArgumentException">The <paramref name="text"/> parameter is longer than 1000 characters.</exception>
/// <exception cref="TranslatorServiceException">The provided <see cref="SubscriptionKey"/> isn't valid or has expired.</exception>
/// <remarks><para>This method perform a non-blocking request for text translation.</para>
/// <para>For more information, go to http://msdn.microsoft.com/en-us/library/ff512421.aspx.
/// <para>For more information, go to https://docs.microsofttranslator.com/text-translate.html#!/default/get_Translate.
/// </para>
/// </remarks>
/// <seealso cref="Language"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************

namespace Microsoft.Toolkit.Uwp.Services.MicrosoftTranslator
{
/// <summary>
/// Holds information about langagues supported for text translation and speech synthesis.
/// </summary>
public class ServiceLanguage
{
/// <summary>
/// Gets the language code.
/// </summary>
public string Code { get; }

/// <summary>
/// Gets the language friendly name.
/// </summary>
public string Name { get; }

/// <summary>
/// Returns the language friendly name.
/// </summary>
/// <returns>The language friendly name.</returns>
public override string ToString() => Name;

/// <summary>
/// Initializes a new instance of the <see cref="ServiceLanguage"/> class, using the specified code and name.
/// </summary>
/// <param name="code">The language code.</param>
/// <param name="name">The language friendly name.</param>
public ServiceLanguage(string code, string name)
{
Code = code;
Name = name;
}
}
}
Loading