Skip to main content
Inspiring
January 13, 2023
Question

How difficult would it be to create a text translator?

  • January 13, 2023
  • 3 replies
  • 1288 views

My HTML/CSS knowledge can best be described as "good enough to get by". I've also (barely) dipped my toe in PHP (mainly to assemble modular pages and use variables).

 

When I was a kid, there was a website that offered to "translate" any block of text into various slangs. For example, Pirate Talk, which would replace all instances of friend or pal with "matey", add lots of "Arrrr"s, etc. Pretty basic stuff, really.

 

I'd like to do something like that : a web page with a large input box which, once submitted, would spit out the same text with a modified slang. I assume this would simply require a bunch of if/then statements : like FRIEND or PAL = MATEY.

 

I would imagine this is easy enough to do in PHP via string search/replace calls, but my PHP knowledge doesn't extend far enough for me to produce the code from scratch. I'd be able to customize it, however. I'm picturing a simple form with an input box, a submit button, and a div with the displayed results -- using a demo search/replace string I can clone to build up a more exhaustive word bank -- would do the trick (if there's a good samaritan out there reading this).

This topic has been closed for replies.

3 replies

Participant
January 17, 2023

sorry 🙏  i didn't get it 😂 .

 

Legend
January 18, 2023
quote

sorry 🙏  i didn't get it 😂 .

 


By @Moneeb27978289smy8

 

Some experience and more so reading posts 2 or 3 times counts, obviously.

Legend
January 14, 2023

@Under S. wrote:

 

I'd like to do something like a web page with a large input box which, once submitted, would spit out the same text with a modified slang. I assume this would simply require a bunch of if/then statements : like FRIEND or PAL = MATEY.

 

I would imagine this is easy enough to do in PHP via string search/replace calls......


 

Its not difficult. See example php code below. There are several ways to do it but having the replacement words in an associative array along side the words they are replacing then looping through the array using 'foreach' and 'str_replace' php functions will be a lot easier to manage.

 

You can split the current/replacement words into 2 arrays but if you have a lot of current/replacement words that will be difficult to manage and keep track of what word replacement word belongs to which current word. 

 

 

<?php

$string = "Hello friend you are a good pal";

$wordBank = [
['current'=>'friend', 'replacement'=>'matey'],
['current'=>'pal', 'replacement'=>'mate']
];

foreach ($wordBank as $word) {
$string = str_replace($word['current'], $word['replacement'], $string);
}

echo "<p>$string</p>";

?>

 

 

 

 

B i r n o u
Legend
January 14, 2023

great, just curious what happen, for mate, buddy, fellow... and so on... does it mean that we will have to feed the duck for each entrance ?

Legend
January 14, 2023
quote

great, just curious what happen, for mate, buddy, fellow... and so on... does it mean that we will have to feed the duck for each entrance ?


By @B i r n o u

 

Of course you would need a word bank to 'compare' words in the user input string against those you want to replace, which is built up over time. 

BenPleysier
Community Expert
Community Expert
January 14, 2023

This may help

https://youtu.be/3QFU7rEgeEM

 

 

Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
Legend
January 14, 2023
quote

This may help

https://youtu.be/3QFU7rEgeEM

 

By @BenPleysier

 

Sad indeed, the youth of today are becoming too thick to think or do anything for themselves. 

Legend
January 14, 2023

@Under S. wrote:

I'd like to do something like a web page with a large input box which, once submitted, would spit out the same text with a modified slang. I assume this would simply require a bunch of if/then statements : like FRIEND or PAL = MATEY.

I would imagine this is easy enough to do in PHP via string search/replace calls......



You dont need php at all for this you can use plain old javascript. Of course you might want to do something with the translated version at server level in which case you will have to involve a server language.

 

Anyway food for thought below is a full javascript version, just add the current and replacement words to the wordBank array and then type away in the top box, click the translate button and the translated version will appear in the bottom box.

 

Of course be mindfull of ALL user input. You'll need to sanitize the input. I think there is a javacript library which can be deployed to do that.

 

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Translate Text</title>
<style>
.inputWrapper {
width: 50%;
margin: 0 auto;
}
.inputText, .translatedText {
min-height: 200px;
padding: 25px;
border: 1px solid #000;
}
.inputText p, .translatedText p {
margin: 0 0 8px 0;
padding: 0;
}
p::first-letter {
text-transform: uppercase;
}
button.translate {
display: block;
margin: 20px auto;
background-color: #000;
color: #fff;
padding: 10px 25px;
border-radius: 6px;
border: none;
}
</style>
</head>
<body>
<div class="inputWrapper">

<div class="inputText" contentEditable="true">
<p>Hello friend you are a good pal</p>
</div>

<button class="translate">Translate</button>
<div class="translatedText"></div>

</div>
<!-- end inputWrapper -->

<script>
/* BUILD WORD BANK */
const wordBank = [
{current: 'friend' , replacement: 'matey'},
{current: 'pal' , replacement: 'mate'},
];

const translate = document.querySelector('.translate');
const translatedText = document.querySelector('.translatedText');

translate.onclick = function() {
let inputText = document.querySelector('.inputText').innerHTML.toLowerCase();
wordBank.forEach(function(word) {
inputText = inputText.replaceAll(word.current, word.replacement);
});
translatedText.innerHTML = inputText;
}

</script>
</body>
</html>