# Translation

The translation library is provided by leveraging Google's Cloud Translation function.
Google's translation API KEY is required to use this library, so if you have not received one
Please proceed by following the guide document below.

If you have already received an API KEY, you can proceed to next paragraph.

# Register API KEY

This function is partially free, but in order to use the translation function, you must first obtain an API KEY to use Google Translate.


# Register your account information

First, log in to Google and go to the place that issues the API (opens new window).
After that, click Try Translation for Free and you will be redirected to the page.

    TIP

    Card registration is not automatically charged, and you may be charged a $1 fee for initial registration, but will be refunded.
    If you don't sign up for automatic billing yourself, you won't be charged additionally when you use up your API quota.


    # Get an API key


      # Registering API KEY to vchatcloud CMS

        # How to use the library

        1. Follow the contents of Overview.

        2. At the top of script.js, write the code below.

        const { Trans } = e7lib;
        // or
        // const Trans = e7lib.Trans;
        
        const translation = new Trans({
          targetTag: '.comment',
          trans: ['ko', 'en', 'de', 'vi', 'es', 'fr', 'pt', 'tr', 'ar', 'it', 'id', 'ru'],
          roomId: channelKey,
        });
        
        1
        2
        3
        4
        5
        6
        7
        8
        9
        1. Check if the translation window appears when right-clicking on the entered element.

        • parameter value (* is required)
        Value Type Description
        targetSelector* String Enter the element's selector (class name/ID). (ex: .class_name, #element_id)
        trans* Array<String> Enter the Language List (opens new window) to set the translation function.
        roomId* String Enter the channel key value used to open a room.

        # Methods

        ※. automatic translation

        • translate(message, lang, roomId)
        Value Type Description
        message* String User-entered message
        lang* String Enter the language in which to perform the translation function. Language List (opens new window)
        roomId* String Enter the channel key value used to open a room.
        // Code for automatic translation
        channel.onNotifyMessage = async function (event) {
          // >> added code, 'selectLang' can be entered directly
          if (getUserInfo(event.clientKey)?.lang && getUserInfo(event.clientKey).lang !== 'none') {
            const selectLang = getUserInfo(event.clientKey).lang
            const result = await trans.translate(event.message, selectLang, channel.roomId);
            event.message = result.data || event.message;
          }
          // <<
          write(event);
        };
        
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        Copyright 2022. E7Works Inc. & JOYTUNE Corp. All Rights Reserved.