| This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (July 2021) Reason: Nothing but a list of variables and a single scratchblocks tag |
A translator is a project which can change words into different languages using the Translate Extension. This tutorial will show how to make a translator. It is useful if one wants to make a translator application in an Operating System project.
Preparation
One sprite will be needed to store all the code:
- Button
Two variables will also be needed to store the language entered as well as the text string the user wants to translate:
(to language)(string)
Two messages will be needed to tell the project when to ask for the language (it will do this after the string is given by the user):
broadcast (original v)broadcast (to what v)
Programming the Translator
Add the following code to the button:
when green flag clicked broadcast (original v) //This broadcasts a message so that the project knows to ask for the word. when I receive [original v] say [Click this button to translate.] wait until <<touching (mouse-pointer v)> and <mouse down?>> //This waits until the button is clicked. say []//This is so it stops saying "Click this button to translate." ask [What shall be translated? Please only enter real words or the translator will not work.] and wait set [string v] to (answer) //This stores the word that the user entered inside of a variable. This is an important part! broadcast (to what v) //This tells the program that it should ask for the language next. when I receive [to what v] ask [To what language?] and wait set [to language v] to (answer) if <(translate (string) to (to language)) = (string)> then //This is to make sure a supported language is entered. say [Please enter a supported language.] //If a supported language isn't entered, this code will say "Please enter a supported language." broadcast (to what v) //This asks the project to ask the user "What language?" again, creating recursion. else say (translate (string) to (to language)) wait until <<touching (mouse-pointer v)> and <mouse down?>> say [] broadcast (original v)
Your translator is now ready!