Google Translate API: Change Destination Language to English

Last Reply 5 months ago By dharmendr

Posted 5 months ago

 

@model News4MediaSite.News4MediaRepository.tbl_News
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
@{
    /**/

    ViewBag.Title = "Edit";
    // Layout = "~/Views/Shared/DashBoardLayOut.cshtml";
}



<meta charset="UTF-8">



<script type="text/javascript" src="http://www.google.com/jsapi"></script>

<script type="text/javascript">
    google.load("elements", "1", { packages: "transliteration" });
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
    function OnLoad() {
        debugger;
        $('[name*=rblLanguage]').on("change", function () {
            var selected = $("input:radio[name=rblLanguage]:checked").val();
            if (selected == "English") {
                var options = {
                    //  sourceLanguage:
                    //  google.elements.transliteration.LanguageCode.ENGLISH,
                    //  destinationLanguage:
                    //   [google.elements.transliteration.LanguageCode.ENGLISH],
                    //  shortcutKey: 'ctrl+g',
                    //  transliterationEnabled: true
                };
                
                var control = new google.elements.transliteration.TransliterationControl(options);
                control.makeTransliteratable(["Description"]);
                return false;
                //var keyVal = 32; // Space key
                //$("#txtEnglish").on('keydown', function (event) {
                //    if (event.keyCode === 32) {
                //        var engText = $("#txtEnglish").val() + " ";
                //        var engTextArray = engText.split(" ");
                //        $("#Description").val($("#Description").val() + engTextArray[engTextArray.length - 2]);

                //        document.getElementById("Description").focus();
                //        $("#Description").trigger({
                //            type: 'keypress', keyCode: keyVal, which: keyVal, charCode: keyVal
                //        });
                //    }
                //});

                //$("#Description").bind("keyup", function (event) {
                //    setTimeout(function () { $("#txtEnglish").val($("#txtEnglish").val() + " "); document.getElementById("Description").focus() }, 0);
                //});

            }
            if (selected == "Hindi") {
                var options = {
                    sourceLanguage:
                    google.elements.transliteration.LanguageCode.ENGLISH,
                    destinationLanguage:
                    [google.elements.transliteration.LanguageCode.HINDI],
                    shortcutKey: 'ctrl+g',
                    transliterationEnabled: true
                };

                var control = new google.elements.transliteration.TransliterationControl(options);
                control.makeTransliteratable(["Description"]);
                var keyVal = 32; // Space key
                $("#txtEnglish").on('keydown', function (event) {
                    if (event.keyCode === 32) {
                        var engText = $("#txtEnglish").val() + " ";
                        var engTextArray = engText.split(" ");
                        $("#Description").val($("#Description").val() + engTextArray[engTextArray.length - 2]);

                        document.getElementById("Description").focus();
                        $("#Description").trigger({
                            type: 'keypress', keyCode: keyVal, which: keyVal, charCode: keyVal
                        });
                    }
                });

                $("#Description").bind("keyup", function (event) {
                    setTimeout(function () { $("#txtEnglish").val($("#txtEnglish").val() + " "); document.getElementById("txtEnglish").focus() }, 0);
                });

            }
           
        });
    }
           // }//end onLoad function

    google.setOnLoadCallback(OnLoad);
	//$(function(){google.setOnLoadCallback(OnLoad)};
</script>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="content-wrapper">
        <section class="content-header">
            <h1>
                Edit News
                <small>@*Preview*@</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> Admin</a></li>
                <li class="active">Edit News</li>
            </ol>
        </section>
        <section class="content">
            <div class="box box-default">
                <div class="box-body">
                    <div class="col-sm-12">
                        <table id="rblLanguage1">
                            <tr>
                                <td>
                                    <input type="radio" name="rblLanguage" value="English" checked /><label for="rblLanguage_0">English</label>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="radio" name="rblLanguage" value="Hindi" /><label for="rblLanguage_1">Hindi</label>
                                </td>
                            </tr>
                        </table>
                        <span><small>(for type in hindi, please type in this textbox,if want to write in english, please copy english text and paste to News Textbox )</small></span>
                        @Html.TextBox("txtEnglish", "", new { @class = "form-control" })
                    </div>
                    <hr />
                    <div class="row">
                        <div class="form-horizontal">
                            <h4></h4>
                            <hr />

                            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                            @Html.HiddenFor(model => model.ID)

                            <div class="col-sm-7">
                                <div class="form-group">


                                    <div class="col-sm-3">News:</div>
                                    @*@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-3" })*@
                                    <div id="descr" class="col-md-9">
                                        @Html.TextAreaFor(m => m.Description, new { @class = "form-control required", @rows = "6" })
                                        @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
                                    </div>
                                </div>

When i first checked hindi radio button , then it types hindi in description txtbox but if i changed it to english, coz google api not convert english to english, then error comes and it remains type in hindi, Pls give me a solution if i select english it remove hindi script from it.

Posted 5 months ago

Hi mukesh1,

As per Google reference you can't set the DestinationLanguage to english. Check the below link of SupportedDestinationLanguages enum section.

https://developers.google.com/transliterate/v1/reference

To change it to english you need to do some work around with .toggleTransliteration().