0
votes

I used the below code. this is worked in the index.html

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

<script type="text/javascript">
    // Load the Google Transliterate API
    google.load("elements", "1", {
      packages: "transliteration"
    });

    function onLoad() {
      if (google.elements.transliteration.isBrowserCompatible()) {
        var options = {
          sourceLanguage: google.elements.transliteration.LanguageCode.ENGLISH,
          destinationLanguage: [google.elements.transliteration.LanguageCode.SINHALESE],
          shortcutKey: 'ctrl+g',
          transliterationEnabled: true
        };

        // Create an instance on TransliterationControl with the required
        // options.
        var control =
          new google.elements.transliteration.TransliterationControl(options);

        // Enable transliteration in the textbox with id
        // 'transliterateTextarea'.
        control.makeTransliteratable(['transliterateTextarea']);
      } else {
        document.getElementById('errorDiv').innerHTML = 'Sorry! Your browser does not support transliteration';
      }
    }

    google.setOnLoadCallback(onLoad);

  </script>

after change above this code.

Ts Component

import { Component, OnInit } from '@angular/core';  
declare var google:any; 

@Component({
selector: 'app-root',
templateUrl:'./app.component.html',
styleUrls: ['./app.component.css'] })

export class AppComponent implements OnInit {   
 title = 'translate';  
 public sinhalaText: string;   

 constructor() { 
         google.load('elements','1', { packages: 'transliteration'});
         google.setOnLoadCallback(this.onLoad);     
 }

 ngOnInit() {}



onLoad() {
     const sinhalOptions = {
       sourceLanguage:
         google.elements.transliteration.LanguageCode.ENGLISH,
       destinationLanguage:
         [google.elements.transliteration.LanguageCode.SINHALESE],
       shortcutKey: 'ctrl+s',
       transliterationEnabled: true
     };
     const tamilOptions = {
       sourceLanguage:
         google.elements.transliteration.LanguageCode.ENGLISH,
       destinationLanguage:
         [google.elements.transliteration.LanguageCode.TAMIL],
       shortcutKey: 'ctrl+t',
       transliterationEnabled: true
     };
     const sinhalaControl = new google.elements.transliteration.TransliterationControl(sinhalOptions);
     const elements = document.getElementsByClassName('sinhalaText');>         sinhalaControl.makeTransliteratable(elements);
     // const sinhalaControl = new google.elements.transliteration.TransliterationControl(sinhalOptions);
     // sinhalaControl.makeTransliteratable(this.sinhalaText);   
    }



 }

html comonent

<textarea [(ngModel)]="sinhalaText" id="sinhalaText" style="width:600px;height:200px"></textarea>

index.html

<body>
  <app-root></app-root>
  <script type="text/javascript" src="https://www.google.com/jsapi"></script>
</body>

This is not working.

That code works on index.html file in angular. but I request that the code be embedded in the inside the component in angular application. how it is do ?

3
What the problem if you put the code of your second script tag into a function of your angular component ? I think the only thing you need to do is to add declare var google: any; at the beginning of your TS file.Gilsdav
i already use this but not working.Prabuddha Namal
Use AfterViewInit and not the constructor. And use ViewChild instead of document.getElementsByClassName('sinhalaText'). PS: you don't have any class named "sinhalaText" but it's an ID.Gilsdav

3 Answers

1
votes

Please use good livecyle function: AfterViewInit that wait HTML is included in DOM.

In TS

@ViewChild('sinhalaTextInput') sinhalaTextInput: ElementRef;

ngAfterViewInit() {
    ...
    google.setOnLoadCallback(() => this.onLoad()); // Don't lose "this" context 
}

private onLoad() {
    ...
    const elements = this.sinhalaTextInput.nativeElement;
    ...
}

In HTML

<textarea #sinhalaTextInput [(ngModel)]="sinhalaText" id="sinhalaText" style="width:600px;height:200px"></textarea>
1
votes

Below code is working for me.

I have done two changes

  1. I have moved below code to index.html inside script tag.
      google.load("elements", "1", {
          packages: "transliteration"
        });
  1. Changed below line in ts file. Please follow the full code mentioned below. Its working.
sinhalaControl.makeTransliteratable([elements.id]);

import { Component, ElementRef, ViewChild, OnInit, AfterViewInit } from '@angular/core';
declare var google: any;
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, AfterViewInit {
  title = 'ng-transliteration';
  sinhalaText: string = '';
  constructor() {
  }
  @ViewChild('sinhalaTextInput', {static: false}) sinhalaTextInput: ElementRef;
  ngOnInit() {

  }
  ngAfterViewInit() {
    google.setOnLoadCallback(() => this.onLoad());
  }
  onLoad() {
    const sinhalOptions = {
      sourceLanguage:
        google.elements.transliteration.LanguageCode.ENGLISH,
      destinationLanguage:
                 [google.elements.transliteration.LanguageCode.SINHALESE],
   shortcutKey: 'ctrl+s',
   transliterationEnabled: true
 };
 const tamilOptions = {
   sourceLanguage:
     google.elements.transliteration.LanguageCode.ENGLISH,
   destinationLanguage:
     [google.elements.transliteration.LanguageCode.TAMIL],
   shortcutKey: 'ctrl+t',
   transliterationEnabled: true
 };
    const sinhalaControl = new google.elements.transliteration.TransliterationControl(sinhalOptions);
    const elements = this.sinhalaTextInput.nativeElement;
    sinhalaControl.makeTransliteratable([elements.id]);
    // const sinhalaControl = new google.elements.transliteration.TransliterationControl(sinhalOptions);
    // sinhalaControl.makeTransliteratable(this.sinhalaText);
   }

}
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>NgTransliteration</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  <script>
    google.load("elements", "1", {
      packages: "transliteration"
    });
  </script>
</head>
<body>
  <app-root></app-root>

</body>
</html>


<!---- app.component.html ---->


<textarea [(ngModel)]="sinhalaText" #sinhalaTextInput id="sinhalaText" style="width:600px;height:200px"></textarea>
0
votes

I just posting full component as answer

export class HomeComponent implements OnInit {
model = {
    left: true,
    middle: false,
    right: false
};
sinhalaText:"string"
focus;
focus1;
constructor() { }

ngOnInit() {}

@ViewChild('sinhalaTextInput') sinhalaTextInput: ElementRef;
ngAfterViewInit() {
    google.load("elements", "1", {
        packages: "transliteration"
      });   

google.setOnLoadCallback(() => this.onLoad()); // Don't lose "this" context 
}

private onLoad() {
const elements = this.sinhalaTextInput.nativeElement;
var options = {
    sourceLanguage:
        google.elements.transliteration.LanguageCode.ENGLISH,
    destinationLanguage:
        [google.elements.transliteration.LanguageCode.TAMIL],
    shortcutKey: 'ctrl+g',
    transliterationEnabled: true
};

// Create an instance on TransliterationControl with the required
// options.
var control =
    new google.elements.transliteration.TransliterationControl(options);

// Enable transliteration in the textbox with id
// 'transliterateTextarea'.
control.makeTransliteratable([elements]);

}


}