Azlier
Old World Ghost
- Reaction score
- 461
Have you ever wanted an RPG map where NPC's that spoke different languages exist? Well, if you define the words, this script can translate English into your language for you.
These languages must follow English grammar rules.
First, here's the script for translating English -> Language. This includes the Language API.
This is a sample script for language definition. Very incomplete.
And last, but not least, a script showcasing how easy it is to translate stuff.
These languages must follow English grammar rules.
First, here's the script for translating English -> Language. This includes the Language API.
JASS:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~ Language ~~ By Azlier ~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// What is Language?
// - Language translates phrases from English to defined languages.
//
// Methods:
// - Language.create() creates a new language that words can be added to.
// Up to 8191 languages can exist.
//
// - languageInstance.translateWord(string word) returns translation
// Translates the given word into its counterpart in the language.
//
// - languageInstance.translate(string phrase) returns translation
// Translates an entire phrase into the language. Can support any string length.
//
// - languageInstance.define(string word, string translation)
// This is used to define the translations for different English words.
// word is case insensitive. translation is CaSe SeNsItIvE.
//
//
// How to import:
// - Create a trigger named Language.
// - Convert it to custom text and replace the whole trigger text with this.
// - Define languages in a separate trigger.
//
// Thanks:
// - quraji for fixing the string parser.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library Language initializer Init
globals
private hashtable ht = InitHashtable()
endglobals
private function AddSeparator takes string s returns nothing
call SaveBoolean(ht, 0, StringHash(s), true)
endfunction
struct Language
method define takes string word, string trans returns nothing
call SaveStr(ht, this, StringHash(word), trans)
endmethod
method translateWord takes string word returns string
//It's important to know if you forgot to define a word.
local string s = LoadStr(ht, this, StringHash(word))
if s == null then
debug if StringLength(word) > 0 then
debug call BJDebugMsg("Language ERROR: Undefined word \"" + word + "\".")
debug endif
return word
endif
return s
endmethod
method translate takes string source returns string
local integer i = 0
local string result = " "
local string s
local integer end = StringLength(source)
local string s2 = source
loop
exitwhen (i==end)
set s = SubString(source, i, i+1)
if LoadBoolean(ht, 0, StringHash(s)) then // separator found
set result = result + .translateWord(SubString(source, 0, i)) // translate the word and add it to the result
set result = result + s // add the separator
set source = SubString(source, i+1, end) // set source to itself minus what has been parsed
set i = -1 // reset i (-1 so it is incremented to 0 after the endif)
endif
set i = i + 1
endloop
if (i>0) then // there's some unparsed string left (with no separators, so should be a word)
set result = result + .translateWord(source)
endif
set s = SubString(s2, 0, 1)
set s2 = StringCase(s, true)
if s == s2 then
set result = StringCase(SubString(result, 0, 2), true) + SubString(result, 2, StringLength(result))
endif
return result
endmethod
endstruct
function CreateLanguage takes nothing returns Language
return Language.create()
endfunction
function DefineWord takes Language l, string w, string t returns nothing
call l.define(w, t)
endfunction
function TranslateWord takes Language l, string s returns string
return l.translateWord(s)
endfunction
function Translate takes Language l, string s returns string
return l.translate(s)
endfunction
private function Init takes nothing returns nothing
//Define separators (spaces, punctuation marks)
call AddSeparator(" ")
call AddSeparator("\"")
call AddSeparator(".")
call AddSeparator("039;")
call AddSeparator(":")
call AddSeparator(";")
call AddSeparator(",")
call AddSeparator("(")
call AddSeparator(")")
call AddSeparator("-")
call AddSeparator("!")
call AddSeparator("?")
call AddSeparator("\\")
call AddSeparator("/")
endfunction
endlibrary
This is a sample script for language definition. Very incomplete.
JASS:
library LanguagePreDef initializer Init requires Language
//This small library is an example of how languages are defined.
//It's tedious work, and you must define every word you want to translate correctly.
globals
Language LANGUAGE_ELVEN
//Just a tiny, tiny predefined language. To show you how it's done.
endglobals
private function Init takes nothing returns nothing
//Init languages:
set LANGUAGE_ELVEN = CreateLanguage()
//Define Elven:
call DefineWord(LANGUAGE_ELVEN, "the", "ëi")
call DefineWord(LANGUAGE_ELVEN, "a", "ai")
call DefineWord(LANGUAGE_ELVEN, "I", "Æi")
call DefineWord(LANGUAGE_ELVEN, "fight", "purya")
call DefineWord(LANGUAGE_ELVEN, "death", "moor")
call DefineWord(LANGUAGE_ELVEN, "prepared", "yawhæt")
call DefineWord(LANGUAGE_ELVEN, "you", "ëiu")
call DefineWord(LANGUAGE_ELVEN, "are", "aiur")
call DefineWord(LANGUAGE_ELVEN, "to", "ag")
call DefineWord(LANGUAGE_ELVEN, "am", "at")
call DefineWord(LANGUAGE_ELVEN, "hill", "hëihl")
call DefineWord(LANGUAGE_ELVEN, "hills", "hëihli")
call DefineWord(LANGUAGE_ELVEN, "at", "yr")
call DefineWord(LANGUAGE_ELVEN, "it", "ëth")
call DefineWord(LANGUAGE_ELVEN, "on", "eun")
call DefineWord(LANGUAGE_ELVEN, "will", "yeihll")
call DefineWord(LANGUAGE_ELVEN, "for", "tor")
call DefineWord(LANGUAGE_ELVEN, "die", "mor")
call DefineWord(LANGUAGE_ELVEN, "fights", "puryæ")
endfunction
endlibrary
And last, but not least, a script showcasing how easy it is to translate stuff.
JASS:
scope LanguageTest initializer Init
//This is a test script for Language.
//Type something like "death to the hill" ingame to see the result.
private function Actions takes nothing returns boolean
call DisplayTextToPlayer(Player(0), 0, 0, "Translated to Elven: " + Translate(LANGUAGE_ELVEN, GetEventPlayerChatString()))
return false
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterPlayerChatEvent(t, Player(0), "", false)
call TriggerAddCondition(t, Condition(function Actions))
endfunction
endscope