User Tools

Site Tools


developer_center:developer_editor:script:autotranslationscript

How to Use Auto Translation on Script

In This Tutorial there is special new API to detect player language.

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

If your Account created in Indonesia and using English language on current Session. It will output this

result = 0 
languageCode = "en"
areaCountry  = "ID" 

Let's Jump Straight into the Tutorial


Tutorial Auto Translation on Script

We will be Installing the following framework just in 2 step

Step 1

The file of this script must be on top of script you are going to use translation.


Step 2

Copy and Paste this Script bellow 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 Player is from Spain
-- Cannot Directly be Used before player joined the game
T_Text(playerid,"Hello World") --> "Hola Mundo"

Translating the String

We are going to use Spreadsheet from Google to define the string translation. So, make sure you have a google account.

Skip Step 1 and 2 if you already did it. And just reuse the Spreadsheet you copied.

Step 1

Go to this Spreadsheet: Auto Translation Spreadsheet by Hhuxx1


Step 2

Clone the Spreadsheet :

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 give 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 - 1, 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 inlineResult = "";
  for (var i = 0; i < dataValues.length; i++) {
    var rowData = dataValues[i];
    if(rowData.some(item => item.trim() === "")){
      break;
    }else{
      for(var l = 0; l<rowData.length; l++){
        //Logger.log(rowData[l])
        inlineResult+=`createText("`+codeLang[l]+`","`+rowData[l]+`","`+rowData[0]+`");`;
      }
    }
  }
  return inlineResult;
}

Click “Run” then it Will Pop Up This.


Then 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

Write your Text in English Column. On your copied spreadsheet.


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 Loading. Wait until the result appear. ;

After the result Appear copy the cell into your script “List of Text” File

Step 5

Paste into your script “List of Text” File. After the “Language Session of Player” file.

developer_center/developer_editor/script/autotranslationscript.txt · Last modified: 2024/07/19 06:48 by hhuxx