¿Matrices PHP o traducción MySQL? ¿Cómo?

I would like to create a page that contains just a textarea and placeholder for the output text. The user would enter text into the textarea and while they type (or when the enter button is pressed) the text is translated (or more to the point - swapped out). For example, I might write something like "In my honest opinion", or "to be honest" and that would convert the text to its abbreviated form IMHO and TBH, I thought about doing this with arrays. The text could be searched for the longer strings first, i.e. I thought about have an array of four word phrases, three word phrases, two word ones and a single array of words that can be abbreviated, they could be in "key" => "value" form. Would this work, or would mysql work better?

preguntado el 28 de agosto de 11 a las 00:08

1 Respuestas

It sounds like you're looking to map full text into abbreviated text. This should work just like actual i18n translation, so any translation implementation would suit.

Even if you don't use Zend Framework, you could look at Zend_Translate adaptadores to see their supported mapping solutions for a suitable candidate.

Really though, beyond telling you that mapping that key => value is definitely the way to go; how you store the map is entirely up to you.


$map = array(
    'in my honest opinion' => 'IMHO',
    'to be honest' => 'TBH'

// Really simple search/replace
$translated = str_replace(array_keys($map), $map, $string_to_translate);

This shows a really simple search/replace, but the complexity of your replacing will depend on the type of input and the type of replacement you expect

Respondido el 20 de junio de 20 a las 12:06

I thought about having the full text as the key and the abbreviation as the value. I don't have much experience with frameworks but when I have done in the past I feel that they aren't always fit for purpose and sometimes cumbersome and too complicated. Is this question not as simple as I thought? As I don't know PHP myself I would like to give it a go from scratch, could this not be done by using a simple search and replace function? - Michael

Also, why did my question get marked down? I don't think I could have put it any other way? - Michael

I didn't suggest for you utilizado Zend_Translate, I meant for you to look at how they map translations. You could absolutely use key => value mapping like that, but whether you store them in an array, MySQL, XML, Gettext, whatever; the choice is yours. You probably got marked down because your question really boils down to "how should I store my data? Array or MySQL". It kinda shows you haven't really researched and realised there isn't really going to be a difference unless you want to manipulate the data - Adlawson

I know how to store the different texts, but I am unsure about the code that does the actual translating/searching. - Michael

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.