Google Translate in SPFx

This article provides steps to implement the Google translation in the SharePoint Framework (SPFx) web part, generally, Google translate is a free multilingual statistical and neural machine translation service developed by Google, to translate text and websites from one language into another. We can access this feature as API. so we can Google translate can be use inside the SPFx too. this article explains that.

Create a new web part project

Open power shell and run following comment to create a new web part by running the Yeoman SharePoint Generator

yo @microsoft/sharepoint

When prompted:

Enter the webpart name as your solution name, and then select Enter.
Select Create a subfolder with solution name for where to place the files.
Select Y to allow the solution to be deployed to all sites immediately.
Select N on the question if solution contains unique permissions.
Select WebPart as the client-side component type to be created.

The next set of prompts ask for specific information about your web part:

Enter your web part name, and then select Enter.
Enter your web part description, and then select Enter.
Select React framework as the framework you would like to use, and then select Enter.

Start Visual Studio Code (or your favorite code editor) within the context of the newly created project folder.

cd .\web part name\
code .

Install the library and required dependencies

npm install translate

Update the react component

Import the npm translate module in the src\wsrc\webparts\spfxGoogletranslate\components\SpfxGoogletranslate.tsx also, you have to get the API key from google to use this service.

import * as React from 'react';
import styles from './SpfxGoogletranslate.module.scss';
import { ISpfxGoogletranslateProps } from './ISpfxGoogletranslateProps';
import { escape } from '@microsoft/sp-lodash-subset';
import translate from 'translate';
import { Stack, IStackProps, IStackStyles } from 'office-ui-fabric-react/lib/Stack';
import { TextField } from 'office-ui-fabric-react/lib/TextField';
import { PrimaryButton } from 'office-ui-fabric-react/lib/Button';
import { autobind } from 'office-ui-fabric-react/lib/Utilities';
import { IDropdownOption, Dropdown } from 'office-ui-fabric-react';


const stackStyles: Partial<IStackStyles> = { root: { width: 650 } };
const stackTokens = { childrenGap: 50 };
const columnProps: Partial<IStackProps> = {
  tokens: { childrenGap: 15 },
  styles: { root: { width: 300 } },
};
const smallcolumnProps: Partial<IStackProps> = {
  tokens: { childrenGap: 15 },
  styles: { root: { width: 180 } },
};

const languages = [
  { key: '', text: 'Select a language' },
  { key: 'ab', text: 'Abkhazian' },
  { key: 'aa', text: 'Afar' },
  { key: 'af', text: 'Afrikaans' },
  { key: 'ak', text: 'Akan' },
  { key: 'sq', text: 'Albanian' },
  { key: 'am', text: 'Amharic' },
  { key: 'ar', text: 'Arabic' },
  { key: 'an', text: 'Aragonese' },
  { key: 'hy', text: 'Armenian' },
  { key: 'as', text: 'Assamese' },
  { key: 'av', text: 'Avaric' },
  { key: 'ae', text: 'Avestan' },
  { key: 'ay', text: 'Aymara' },
  { key: 'az', text: 'Azerbaijani' },
  { key: 'bm', text: 'Bambara' },
  { key: 'ba', text: 'Bashkir' },
  { key: 'eu', text: 'Basque' },
  { key: 'be', text: 'Belarusian' },
  { key: 'bn', text: 'Bengali' },
  { key: 'bh', text: 'Bihari languages' },
  { key: 'bi', text: 'Bislama' },
  { key: 'bs', text: 'Bosnian' },
  { key: 'br', text: 'Breton' },
  { key: 'bg', text: 'Bulgarian' },
  { key: 'my', text: 'Burmese' },
  { key: 'ca', text: 'Catalan, Valencian' },
  { key: 'km', text: 'Central Khmer' },
  { key: 'ch', text: 'Chamorro' },
  { key: 'ce', text: 'Chechen' },
  { key: 'ny', text: 'Chichewa, Chewa, Nyanja' },
  { key: 'zh', text: 'Chinese' },
  { key: 'cu', text: 'Church Slavonic, Old Bulgarian, Old Church Slavonic' },
  { key: 'cv', text: 'Chuvash' },
  { key: 'kw', text: 'Cornish' },
  { key: 'co', text: 'Corsican' },
  { key: 'cr', text: 'Cree' },
  { key: 'hr', text: 'Croatian' },
  { key: 'cs', text: 'Czech' },
  { key: 'da', text: 'Danish' },
  { key: 'dv', text: 'Divehi, Dhivehi, Maldivian' },
  { key: 'nl', text: 'Dutch, Flemish' },
  { key: 'dz', text: 'Dzongkha' },
  { key: 'en', text: 'English' },
  { key: 'eo', text: 'Esperanto' },
  { key: 'et', text: 'Estonian' },
  { key: 'ee', text: 'Ewe' },
  { key: 'fo', text: 'Faroese' },
  { key: 'fj', text: 'Fijian' },
  { key: 'fi', text: 'Finnish' },
  { key: 'fr', text: 'French' },
  { key: 'ff', text: 'Fulah' },
  { key: 'gd', text: 'Gaelic, Scottish Gaelic' },
  { key: 'gl', text: 'Galician' },
  { key: 'lg', text: 'Ganda' },
  { key: 'ka', text: 'Georgian' },
  { key: 'de', text: 'German' },
  { key: 'ki', text: 'Gikuyu, Kikuyu' },
  { key: 'el', text: 'Greek (Modern)' },
  { key: 'kl', text: 'Greenlandic, Kalaallisut' },
  { key: 'gn', text: 'Guarani' },
  { key: 'gu', text: 'Gujarati' },
  { key: 'ht', text: 'Haitian, Haitian Creole' },
  { key: 'ha', text: 'Hausa' },
  { key: 'he', text: 'Hebrew' },
  { key: 'hz', text: 'Herero' },
  { key: 'hi', text: 'Hindi' },
  { key: 'ho', text: 'Hiri Motu' },
  { key: 'hu', text: 'Hungarian' },
  { key: 'is', text: 'Icelandic' },
  { key: 'io', text: 'Ido' },
  { key: 'ig', text: 'Igbo' },
  { key: 'id', text: 'Indonesian' },
  { key: 'ia', text: 'Interlingua (International Auxiliary Language Association)' },
  { key: 'ie', text: 'Interlingue' },
  { key: 'iu', text: 'Inuktitut' },
  { key: 'ik', text: 'Inupiaq' },
  { key: 'ga', text: 'Irish' },
  { key: 'it', text: 'Italian' },
  { key: 'ja', text: 'Japanese' },
  { key: 'jv', text: 'Javanese' },
  { key: 'kn', text: 'Kannada' },
  { key: 'kr', text: 'Kanuri' },
  { key: 'ks', text: 'Kashmiri' },
  { key: 'kk', text: 'Kazakh' },
  { key: 'rw', text: 'Kinyarwanda' },
  { key: 'kv', text: 'Komi' },
  { key: 'kg', text: 'Kongo' },
  { key: 'ko', text: 'Korean' },
  { key: 'kj', text: 'Kwanyama, Kuanyama' },
  { key: 'ku', text: 'Kurdish' },
  { key: 'ky', text: 'Kyrgyz' },
  { key: 'lo', text: 'Lao' },
  { key: 'la', text: 'Latin' },
  { key: 'lv', text: 'Latvian' },
  { key: 'lb', text: 'Letzeburgesch, Luxembourgish' },
  { key: 'li', text: 'Limburgish, Limburgan, Limburger' },
  { key: 'ln', text: 'Lingala' },
  { key: 'lt', text: 'Lithuanian' },
  { key: 'lu', text: 'Luba-Katanga' },
  { key: 'mk', text: 'Macedonian' },
  { key: 'mg', text: 'Malagasy' },
  { key: 'ms', text: 'Malay' },
  { key: 'ml', text: 'Malayalam' },
  { key: 'mt', text: 'Maltese' },
  { key: 'gv', text: 'Manx' },
  { key: 'mi', text: 'Maori' },
  { key: 'mr', text: 'Marathi' },
  { key: 'mh', text: 'Marshallese' },
  { key: 'ro', text: 'Moldovan, Moldavian, Romanian' },
  { key: 'mn', text: 'Mongolian' },
  { key: 'na', text: 'Nauru' },
  { key: 'nv', text: 'Navajo, Navaho' },
  { key: 'nd', text: 'Northern Ndebele' },
  { key: 'ng', text: 'Ndonga' },
  { key: 'ne', text: 'Nepali' },
  { key: 'se', text: 'Northern Sami' },
  { key: 'no', text: 'Norwegian' },
  { key: 'nb', text: 'Norwegian Bokmål' },
  { key: 'nn', text: 'Norwegian Nynorsk' },
  { key: 'ii', text: 'Nuosu, Sichuan Yi' },
  { key: 'oc', text: 'Occitan (post 1500)' },
  { key: 'oj', text: 'Ojibwa' },
  { key: 'or', text: 'Oriya' },
  { key: 'om', text: 'Oromo' },
  { key: 'os', text: 'Ossetian, Ossetic' },
  { key: 'pi', text: 'Pali' },
  { key: 'pa', text: 'Panjabi, Punjabi' },
  { key: 'ps', text: 'Pashto, Pushto' },
  { key: 'fa', text: 'Persian' },
  { key: 'pl', text: 'Polish' },
  { key: 'pt', text: 'Portuguese' },
  { key: 'qu', text: 'Quechua' },
  { key: 'rm', text: 'Romansh' },
  { key: 'rn', text: 'Rundi' },
  { key: 'ru', text: 'Russian' },
  { key: 'sm', text: 'Samoan' },
  { key: 'sg', text: 'Sango' },
  { key: 'sa', text: 'Sanskrit' },
  { key: 'sc', text: 'Sardinian' },
  { key: 'sr', text: 'Serbian' },
  { key: 'sn', text: 'Shona' },
  { key: 'sd', text: 'Sindhi' },
  { key: 'si', text: 'Sinhala, Sinhalese' },
  { key: 'sk', text: 'Slovak' },
  { key: 'sl', text: 'Slovenian' },
  { key: 'so', text: 'Somali' },
  { key: 'st', text: 'Sotho, Southern' },
  { key: 'nr', text: 'South Ndebele' },
  { key: 'es', text: 'Spanish, Castilian' },
  { key: 'su', text: 'Sundanese' },
  { key: 'sw', text: 'Swahili' },
  { key: 'ss', text: 'Swati' },
  { key: 'sv', text: 'Swedish' },
  { key: 'tl', text: 'Tagalog' },
  { key: 'ty', text: 'Tahitian' },
  { key: 'tg', text: 'Tajik' },
  { key: 'ta', text: 'Tamil' },
  { key: 'tt', text: 'Tatar' },
  { key: 'te', text: 'Telugu' },
  { key: 'th', text: 'Thai' },
  { key: 'bo', text: 'Tibetan' },
  { key: 'ti', text: 'Tigrinya' },
  { key: 'to', text: 'Tonga (Tonga Islands)' },
  { key: 'ts', text: 'Tsonga' },
  { key: 'tn', text: 'Tswana' },
  { key: 'tr', text: 'Turkish' },
  { key: 'tk', text: 'Turkmen' },
  { key: 'tw', text: 'Twi' },
  { key: 'ug', text: 'Uighur, Uyghur' },
  { key: 'uk', text: 'Ukrainian' },
  { key: 'ur', text: 'Urdu' },
  { key: 'uz', text: 'Uzbek' },
  { key: 've', text: 'Venda' },
  { key: 'vi', text: 'Vietnamese' },
  { key: 'vo', text: 'Volap_k' },
  { key: 'wa', text: 'Walloon' },
  { key: 'cy', text: 'Welsh' },
  { key: 'fy', text: 'Western Frisian' },
  { key: 'wo', text: 'Wolof' },
  { key: 'xh', text: 'Xhosa' },
  { key: 'yi', text: 'Yiddish' },
  { key: 'yo', text: 'Yoruba' },
  { key: 'za', text: 'Zhuang, Chuang' },
  { key: 'zu', text: 'Zulu' }
];

interface ISpfxGoogletranslateState {
  toLanguage: string;
  content: string;
  userinput: string;
}

export default class SpfxGoogletranslate extends React.Component<ISpfxGoogletranslateProps, ISpfxGoogletranslateState> {
  constructor(props: ISpfxGoogletranslateProps, state: ISpfxGoogletranslateState) {
    super(props);
    this.state = ({ toLanguage: '', content: '', userinput: '' })
    this._translate();
  }

  private async _translate() {
    const result = await translate(this.state.userinput, { to: this.state.toLanguage, engine: 'google', key: 'YOUR-KEY-HERE' });
    this.setState({ content: result })
  }

  public render(): React.ReactElement<ISpfxGoogletranslateProps> {
    return (
      <div className={styles.spfxGoogletranslate}>
        <Stack horizontal tokens={stackTokens} styles={stackStyles}>
          <Stack {...columnProps}>
            <TextField label="English" multiline autoAdjustHeight onChanged={(newtext) => { this.setState({ userinput: newtext }); this._translate() }} />
          </Stack>
          <Stack {...smallcolumnProps}>
            <Dropdown
              placeholder="Select a language"
              label="Select Language"
              options={languages}
              onChanged={(value) => { this.setState({ toLanguage: value.key.toString() }); this._translate() }}
            />

          </Stack>
          <Stack {...columnProps}>
            <label>{this.state.content}</label>
          </Stack>
        </Stack>
      </div>
    );
  }
}

Deploy the solution

You’re now ready to build, bundle, package, and deploy the solution.

Run the gulp commands to verify that the solution builds correctly.

gulp build

Use the following command to bundle and package the solution.

gulp bundle --ship
gulp package-solution --ship

Browse to the app catalog of your target tenant and upload the solution package. You can find the solution package under the sharepoint/solution folder of your solution. It is the .sppkg file. After you upload the solution package in the app catalog. you can find and the web part anywhere across the tenant.

Sharing is caring!

If you have any questions, feel free to let me know in the comments section.
Happy coding!!!

4 thoughts on “Google Translate in SPFx

  1. David Graham June 19, 2020 / 1:04 am

    I’m getting an error, ‘Cannot find module ‘office-ui-fabric-react/lib/Stack’ or its corresponding type declarations.’

    Like

Comments

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s