For experimental reasons, I am trying to convert user input into a fictitious language. All of the translation can be 1:1.
I would prefer if I could accomplish this with PHP.
Should I use gettext and poedit? The fictitious language is not very large, I imagine I can do most of the translation 1:1
I have scoured the googles, but have only found dated code and localization.
2
gettext
is the wrong tool for that job. It is intended to translate a potentially large but finite well-defined set of pre-formulated messages, possibly with variable insertions, between languages. User input in natural language is not such a set. You are better off defining the transformations you need to make in general terms and apply them to the input you get, e.g. as a set of regex transformations stored in a map of from-to pairs. That is easily done in PHP.
But note that there will almost certainly be things that you cannot achieve with such simple replacements. “you all” may usually be transform to “y’all”, but what about “Let me show you all our biggest sellers”? Depending on how convincing you need the result to be, the task may be stupendously more difficult than it appears at first sight. Natural language translation, even if it’s only a “dialect”, is a T. Rex in sheep’s clothing.
3
I did this for a chat program once. You dont need a toolkit. Just parse the text thats entered (ie, separate by spaces) and keep the words in an array. Then start working through them, looking at the words and any subsequent words. Match the sequences with your dialect/slang, and for any match, do the substitution. So as you work through the words, you’d build the output string either from the original word, or from any substitutions you found.
Might help to maintain two arrays – one that maintains the case and punctuation, and the other that converts text to lower case to make your checks against the dictionary simpler. Make sure their array indexes match. This way when you dont find a match in the array with lower case text, you can output the original, unmodified text.
For the dictionary, in PHP, you can take advantage of its arrays – perhaps build it like $words['find'] = 'replace'
. Or for multi-word sequences, make it a nested array.
That should get you a decent first pass at a translator. You’ll probaby then have to consider how to handle things like “text in quotes” and other punctuation.
You are looking for something like this chef/jive/val/pig translator (there is c/lex source there).
The piglatin translator works through each word and reorders the characters.
The jive and val translators identify certain entire words and replaces them with alternate entire words (with some randomization tossed in at times).
http://www.ibm.com/developerworks/linux/library/l-perl-parsing/ works on translating a lex grammar into perl (and incidentally, works on the chef.l file from the first link). It links the perl source for a completed translation which may be of use for a php programmer.
1