프로젝트/번역 나래이터 만들기

Kakao 번역 API와 .NET 음성 Speech를 이용한 번역 나래이터 만들기[WPF] - 3. 최종, 번역 나래이터 구현

언제나휴일 2020. 5. 1. 09:06
반응형

 

번역 나래이터 만들기 프로젝트 마지막 파트입니다.
앞에서 만든 래핑한 Kakao 번역 라이브러리와 번역 음성 Speech 라이브러리를 참조하여 WPF 번역 나래이터를 만드는 실습입니다.

두 개의 라이브러리를 참조 추가한 후에 작성하셔야 합니다.

번역 나래이터 라이브러리.zip
0.01MB

실행 화면 및 배치

소스 코드

MainWindow.xaml

<Window x:Class="번역_나래이터_v0._1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:번역_나래이터_v0._1"
        mc:Ignorable="d"
        Loaded="Window_Loaded"
        Title="번역 나래이터" Height="200" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="5*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ListBox Name="lbox_narbut"/>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="5*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBox Name="tbox_src"/>
            <Button Grid.Column="1" Name="btn_speek"
                    Content="읽기" Click="btn_speek_Click"/>
            <Button Grid.Column="2" Name="btn_translate"
                    Content="번역" Click="btn_translate_Click"/>
        </Grid>
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="5*"/>
                <ColumnDefinition Width="2*"/>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="1" x:Name="btn_speek2"
                    Content="읽기" Click="btn_speek2_Click"/>
            <TextBlock x:Name="tbl_trnas"/>
        </Grid>
        <Button x:Name="btn_all" IsDefault="True"
                Click="btn_all_Click" Content="번역/읽기"
                Grid.RowSpan="3" Grid.Column="1"/>
    </Grid>        
</Window>

MainWindow.cs

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using WrapKakaoLib;
using WrapSpeechLib;

namespace 번역_나래이터_v0._1
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn_speek_Click(object sender, RoutedEventArgs e)
        {
            Grid grid = lbox_narbut.Items[0] as Grid;
            if(grid == null)
            {
                return;
            }
            Narrator narrator = grid.Tag as Narrator;
            narrator.SpeakAsync(tbox_src.Text);
        }

        private void btn_translate_Click(object sender, RoutedEventArgs e)
        {
            string result = KakaoTransAPI.Translate(tbox_src.Text);
            tbl_trnas.Text = result;
        }

        private void btn_speek2_Click(object sender, RoutedEventArgs e)
        {
            Grid grid = lbox_narbut.Items[1] as Grid;
            if(grid == null)
            {
                return;
            }
            Narrator narrator = grid.Tag as Narrator;
            narrator.SpeakAsync(tbox_src.Text);
        }

        private void btn_all_Click(object sender, RoutedEventArgs e)
        {
            Grid grid = lbox_narbut.Items[0] as Grid;
            if(grid == null)
            {
                return;
            }
            Narrator narrator = grid.Tag as Narrator;
            narrator.SpeakAsync(tbox_src.Text);

            string result = KakaoTransAPI.Translate(tbox_src.Text);
            tbl_trnas.Text = result;

            grid = lbox_narbut.Items[1] as Grid;
            if (grid == null)
            {
                return;
            }
            Narrator narrator2 = grid.Tag as Narrator;
            narrator2.SpeakAsync(tbox_src.Text);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<Narrator> narrators = Narrator.Narrators;
            Grid grid;
            ColumnDefinition cd;
            TextBlock tb_bar;
            Button button;
            foreach(Narrator narrator in narrators)
            {
                grid = new Grid();
                grid.Width = lbox_narbut.ActualWidth - 20;
                cd = new ColumnDefinition();
                cd.Width = new GridLength(5, GridUnitType.Star);
                grid.ColumnDefinitions.Add(cd);
                cd = new ColumnDefinition();
                cd.Width = new GridLength(1, GridUnitType.Star);
                grid.ColumnDefinitions.Add(cd);

                tb_bar = new TextBlock();
                tb_bar.Text = narrator.Name;
                grid.Children.Add(tb_bar);
                button = new Button();
                button.Tag = narrator;
                button.Content = "소개하기";
                button.Click += Button_Click;
                grid.Children.Add(button);
                Grid.SetColumn(button, 1);
                lbox_narbut.Items.Add(grid);
                grid.Tag = narrator;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if(button == null)
            {
                return;
            }
            Narrator narrator = button.Tag as Narrator;
            if(narrator == null)
            {
                return;
            }
            narrator.IntroduceAsync();
        }
    }
}

 

 

Kakao 번역 API와 .NET 음성 Speech를 이용한 번역 나래이터 만들기 4. 최종 번역 나래이터 구현[ WPF] –

안녕하세요. 언제나 휴일에 언휴예요. 이제 앞에서 작성한 두 개의 라이브러리를 참조하여 WPF 번역 나래이터를 만들어 보아요. 화면 배치 제일 먼저 화면 배치를 합니다. 배치 Window의 Content를 Gri

ehpub.co.kr

 

반응형