Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
Do you have the complete word list for the fictitious language or are you generating it on the fly? I wouldnt bother with gettext unless you are actually trying to localize a program for the language. –  GrandmasterB Sep 26 '12 at 7:34
I am generating it on the fly. ex. converting northern English to a southern dialect. Replacing 'you all' with 'y'all' etc. What should I use instead of gettext to accomplish this on the fly? –  Alex Waters Sep 26 '12 at 7:35
add comment

3 Answers

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.

share|improve this answer
thank you for the in-depth response. Your answer definitely makes sense. I'm surprised that I can't find any up-to-date dialect translators that operate on user data. I wonder if microsofttranslator.com/dev might be a good place to start... –  Alex Waters Sep 26 '12 at 7:54
It seems that you cannot use your own dictionaries with the Microsoft translator or google translator APIs. I suppose regex transformations are my only hope =( –  Alex Waters Sep 26 '12 at 8:03
so far I have this: codepad.org/uPHHhQBN as the 1:1 conversion method. For this part, should I be using gettext - and then for the grammar create regex? Or should I avoid gettext altogether and use the above for 1:1? –  Alex Waters Oct 3 '12 at 5:13
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
For a southern drawl lex filter, see obairlann.net/reaper/filters/source/drawl.l –  MichaelT Sep 26 '12 at 17:42
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.