Table of Contents

How to Use Auto Translation on Script

In this tutorial, we will show a special new API to detect player language.

local result,languageCode,areaCountry = Player:GetLanguageAndRegion(playerid);

If the player's account was created in Indonesia and they're currently using the English language on their current session, this will be the output:

-- Local Hosted Server 
result = 0 
languageCode = "en"
areaCountry  = "ID" 
 
-- Cloud Server 
result = 0 
languageCode = 1
areaCountry  = nil

Let's jump straight into the tutorial.


Tutorial Auto Translation on Script

We will be installing the following framewor in just two steps.

Step 1

The file of this script must be on top of the script you are going to use for translations.


Step 2

Copy and paste this script below into “Language Session of Player”

playerSession = {}
ScriptSupportEvent:registerEvent("Game.AnyPlayer.EnterGame",function(e)
    local playerid = e.eventobjid;
    local r,lc,ar = Player:GetLanguageAndRegion(playerid);
    local codeLang = {"en", "cn", "tw", "tha", "esn", "ptb", "fra", "jpn", "ara", "kor", "vie", "rus", "tur", "ita", "ger", "ind", "msa"};
    if(r==0)then 
        local inlangcode = tonumber(lc);
            if(inlangcode and inlangcode>=0)then  
                playerSession[playerid]={lc=codeLang[lc+1],ar=""} or {lc="cn",ar=""}
            else  
                playerSession[playerid]={lc=lc,ar=ar};
            end
        else 
        playerSession[playerid]={lc="en",ar="EN"};
    end 
end)
 
T_Text = {};
function toIndex(nonIndex)
    return string.gsub(nonIndex," ","_");
end 
function getSession(playerid)
    return playerSession[playerid].lc
end 
T_Text_meta = {
    __index = function(table,key)
        T_Text[key]={};
        return T_Text[key];
    end,
    __add = function(a, b)
        a[toIndex(b[3])][b[1]] = b[2]
        return a[toIndex(b[3])][b[1]];
    end,
    __sub = function(a,b)
        a[toIndex(b[2])][b[1]] = nil;
    end,
    __call = function(T_Text,playerid,key)
        if T_Text[toIndex(key)][getSession(playerid)] == nil then 
            --Chat:sendSystemMsg("#RERROR T_Text "..toIndex(key).." is Not Defined!",1029380338);
            --print("Session from [",playerid,"] got an Error",getSession(playerid));
            --print("What is T ?",T_Text);
            return key
        end 
        return T_Text[toIndex(key)][getSession(playerid)]
    end
}
T_Text = setmetatable(T_Text,T_Text_meta);
 
function createText(langcode,value,keystring) return T_Text + {langcode,value,keystring}; end

Now you can use it easily like this example:

-- Let's assume the player is from Spain.
-- This cannot be used directly before they join the map.
T_Text(playerid,"Hello, World!") --> "¡Hola, mundo!"

Translating the String

We are going to use Google Sheets to define the string translations. So, make sure you have a Google account (most people already have one anyway).

Skip step 1, 2, and 3 if you already did this. And just reuse the spreadsheet you copied.

Step 1

Go to this spreadsheet: Auto Translation Spreadsheet by Hhuxx1


Step 2

Now, clone it:

File > Make a copy


Step 3

Save it to your drive:

IMPORTANT: If this is your first time using the spreadsheet:

On the spreadsheet you just copied, open the Appscript.

You need to allow the Appscripts by clicking the “ConvertToLuaProject” then giving it authorization by running it once. Make sure it has this specific Code:

 function getResult() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName('LangToTranslate');
  var lastRow = sheet.getLastRow();
  var lastColumn = sheet.getLastColumn();
 
  // start from row 4 (skipping headers)
  var dataRange = sheet.getRange(4, 1, lastRow - 3, lastColumn);
  var dataValues = dataRange.getValues();
  const codeLang = ["en", "cn", "tw", "tha", "esn", "ptb", "fra", "jpn", "ara", "kor", "vie", "rus", "tur", "ita", "ger", "ind", "msa"];
  var result = [];
 
  for (var i = 0; i < dataValues.length; i++) {
    var rowData = dataValues[i];
    if (rowData.some(item => item.trim() === "")) {
      break;
    } else {
      var rowResult = [];
      for (var l = 1; l < rowData.length; l++) { // Start from 1 to skip the first element in rowData
        rowResult.push(`createText("${codeLang[l]}","${rowData[l]}","${rowData[0]}");`);
      }
      result.push(rowResult);
    }
  }
 
  return result;
}

Click “Run”, then this will pop up.


Now, click on Review Permission. Login to your Google account. Once you see this screen:


Click on Advanced. Then click on Go to ConvertLuaProject(unsafe) .


Click on Allow.

Step 3

On your copied spreadsheet, write your text in the English column.


Step 4

Go to sheet named “Result”.

Then write “=getResult()” at any cell to get the result of your translation.

Don't use the function into English column on sheet “LangToTranslate”!

Don't change the sheet “LangToTranslate” name.

It will show as Loading. Wait until the result appears. ;

After the result appears, copy the cell into your script “List of Text” file.

Step 5

Paste it into your script file named “List of Text”. It must be after the “Language Session of Player” file.

Use Shift + Ctrl + Right + Down to block all the results and safely copy it.