Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Code Snippets

Shiva-friendly automatic language translations

Paste down any little snippets or request a new one.

Shiva-friendly automatic language translations

Postby psychicsoftware » 25 Nov 2011, 13:19

I recently wrote a Javascript-based tool which uses the Google Translate Ajax API to translate English phrases, as pasted into a textbox, into other languages. Pretty handy for games. Although the results from Google Translate can be a bit dodgy, I have found that they’re good enough to get version 1 done, and hopefully some interested player will come forward and offer to make improvements (they have for me, anyway).

Each English phrase should be separated by a line-break, and there’s two output options:

1. If you choose a single translation language, you get the English and other language phrases interleaved in your output. So you can store each language in a separate file. This is what I used for my Darkwind client translations.

2. If you click ‘all to XML’ you’ll get a node per phrase, with each language’s translation as a parameter of the node. I have been using this in my Shiva games.. since Shiva data files are always in XML format, this gives me exactly what I need.

The HTML/Javascript page is here

And here’s a Shiva function to take a phrase and return its translation (it’s assumed that GameMainAI has a member variable called ‘sLanguage’ which is the target language, e.g. “uk”, “de”, “es”, “it”, “pt”). Just pass it the English phrase and a handle to the XML table holding the translations.. hopefully it will be useful to some other people here :-)

Code: Select all
--------------------------------------------------------------------------------
function GameMainAI.doLocaliseOneString ( sEnglish, hXMLTable )
--------------------------------------------------------------------------------

local lang = this.sLanguage ( )
local sTranslated = ""

if ( lang=="uk" ) then
   -- special case: English
   sTranslated = sEnglish
else
   local hRoot = xml.getRootElement ( hXMLTable )
   local count = xml.getElementChildCount ( hRoot )
   if ( count > 0 ) then
      for i=0, count-1 do
         local hElem = xml.getElementChildAt ( hRoot, i )
         local sPhrase = xml.getElementValue ( hElem )
         if ( sPhrase==sEnglish ) then
            local hAttrib = xml.getElementAttributeWithName ( hElem, lang )
            if ( hAttrib ) then
               sTranslated = xml.getAttributeValue ( hAttrib )
            end
            break
         end
      end
   end
end

if (string.getLength ( sTranslated )<1) then
   sTranslated = sEnglish -- something went wrong, so revert to the English original
end

return sTranslated



---------------------------------------------------------------------
end
---------------------------------------------------------------------
User avatar
psychicsoftware
Gold Boarder
Gold Boarder
 
Posts: 286
Location: Galway, Ireland

Re: Shiva-friendly automatic language translations

Postby shivachristy » 24 Jan 2013, 11:38

the html page is throwing an error both in ie and safari.

just found out

Google translate is no longer free! At least not for API calls.
User avatar
shivachristy
Platinum Boarder
Platinum Boarder
 
Posts: 959
Location: UK


Return to Code Snippets