PHP language switcher? - php

Does somebody knows a simple php language switcher. I'm not really a PHP savvy and I would like your help.
Thanks in advance.

The answer has already been posted, but let me give a brief explanation here.
Computers aren't smart. They don't understand higher level concepts like language. The fact is: computers can't look at a sentence and know what it means. Using advanced math and algorithms we can dissect the sentence and try to recognize key words, but something as simple as a misspelling could throw the whole algorithm for a loop.
Web services which perform automatic translation are not only buggy, but also tend to require LOTS of power and resources. That's why they're often only owned and operated by companies like Yahoo! (Babelfish) or Google (Google Translate).
Whenever a website has a simple feature for changing language (phpBB has a feature like this built in) the simple fact is that they typed everything several times. Once in English, one in Spanish, once in German... Then by clicking a button it determines whether to send you the English text, Spanish text, or German text. The same is true of wikipedia. When you view an article in two different languages they are not by ANY means the same article. Many times I'll read the Spanish wiki and the information will differ drastically. Two different people wrote two different articles, and by selecting a language you're just telling wikipedia which article to send you.
Your best bet if you really need your website translated at the click of a button is to add Google's Translate Tools. http://translate.google.com/translate_tools

There's no free ride here. You'll have to provide translated strings for every message displayed by your program. This article will get you started: Internationalization in PHP 5.3

yeah it requires some modifications
1. seperate your business logic from presentation layer via templating
2. in your presentation layer remove hard-coded text and replace it with php-variables
3. create your language files
4. depending on how you solved the case, puzzle your app together(hand over the data from the language files in the presentation layer)

Related

Best practice for dynamically translating content into different languages

I am the project manager on a website that needs to be converted into multiple languages. I am trying to figure out what the best option to go with is. I don't have a problem paying for something, but I just want to make sure it will work properly.
The options that I have thought of was to either (somehow) integrate google translate that when the user clicks on the language they want to read the page in, it updates the language for google to translate into. I did work with Google translate a little bit, but I found it to be little clumsy. Maybe I am not using it properly.
Another alternative I had, definitely not the best idea, but a backup if need be is to have the content put in a database and pulling the content dependent on the user's language. The only problem I have is that changing one word on the English version would have to change on every other language.
I am open to any other idea. I can clarify the project more, if need be.
As someone who speaks several languages, I can assure you that Google Translate often misses the mark. In many cases their translations are embarrassing, especially when you try to translate individual words or phrases without a sufficient context. Some language pairs are better than others, but overall this is not an option at this point.
Compiled languages have an advantage of static i18n, when a different version of a code is compiled for each UI language.
Database-driven dynamic i18n is a bad option, and almost all programming frameworks try to avoid it. I would recommend, therefore, that you look for an i18n solution that works with properties (text) files to lookup translated strings. In PHP this is gettext or intl.
Note also that i18n involves not only translation of text, but it also requires appropriate localization of dates, numbers, currencies, etc.
I don't have a problem paying for something, but I just want to make
sure it will work properly.
Based on that statement of yours I would like to suggest that hiring a firm that specializes in translation will be your best bet, then just put a multiple links that will lead to multiple languages of your website.
Problems that you might encounter:
Adjusting contents, some translations might be too short, some might be too long.
Using google translate can ruin your site, because sometimes it fails especially for some languages.

Adding multiple languages in a website

I want to add multiple languages to my site.
I read somewhere that I can use translator(Google or babelfish) but I don't like this way.
Can anyone suggest me different ways?
You could learn the language and translate it yourself. Besides that you will need to use a translator.
You'll want to read up a bit on internationalization and localization (often referred to as i18n L10n). You'll need code to support serving your various translatinons, based on your users' preferences. You'll also want to give some thought to handling things like date and currency formats.
As far as PHP tools, you've got the gettext stuff, which can be compiled in to PHP. Gettext works, but was designed to handle translating interface text for locally-installed software -- it doesn't transition to web sites/apps terribly well.
There's also Zend_Translate, which is a pretty good library, and can easily be used without most of the rest of the Zend Framework. You might want to look at Zend_Locale and Zend_Date, as the three can play together nicely.
You could integrate a translation interface to your site and let the users of your site create their own translation. This way, you get the translation for free.
Or, as an alternative, you could open your website logic to a community (i.e. make it open source) and let it translate by them...
Another way would be to hire someone to translate it into their language :)
if you have members in your site, do what FB is doing ..
they ask the members to help translating to their language, they put the phrases for them, and collect the translations + votes (whether the translation is good or there's better translation).

Adding elements to other elements

I have been using .net for the past couple of years, and I like the way you can add controls at any point in the page from anywhere. For example, you can say Head.Controls.add(new LiteralControl("<link rel='stylesheet' type='text/css' href='styles.css' />")) even if there is already a body.
Is it possible to do this kind of thing in php? My site is set up so that we have agents, customers and artists (this is for a card manufacturing company who wants a customer application for use offline at tradeshows). The add agent form may have different styles to the add customer form. I want to therefore have each type in a different folder (for example agents, customers and artists) each with their own stylesheet. There will be one form page which takes GET parameters of type (artist, customer etc), mode (create, edit) and an optional parameter of ID (when in edit mode). I would like to be able to call $agentForm->generateForm() and $agentForm->generateStyleTag() in one go, rather than what I am currently doing which is to call $agentForm->generateForm() in the body and $agentForm->generateStyleTag when in the head (without even the start body tag being generated yet).
A good way to put this is that I have a Head tag and a Body tag. In another function called $agentForm->generateHTML() I want to say Body->addChild("bla") and Head->addChild("bla"). This makes developing a new page a lot easier since it ensures the styles are there and are correct for the section of the site the user is in.
Is it possible to achieve this, or is this one of the major differences between php and .net?
PHP is in essence designed just to throw strings into STDOUT, unless buffered. To make this work, you'd have to use a templating system that supports this, or roll your own. There is no 'built-in' templating system unless you count loading the whole thing in DOMDocumentor something, and do major raw DOM manipulations, which would be quite a lot slower, and which I wouldn't recommend unless you plan to do a whole lot of other DOM modifications. Google around for PHP templating systems and check which one supports your needs.
You need to realize something. .NET is a framework, not a programming language. The question is identical to Can you do this in C#. Realize that you're comparing Apples to Oranges.
The PHP language is Turing complete, so yes you can do it. That doesn't mean the code to do it is written yet (it may be) or that it will be easy (who knows). It only means it's possible.
With that said, you need to find a framework to do what you want. No language will come with that kind of operation (since it is far too limiting to the language as a whole). So your task is no longer "Can PHP do this", but "I need to find a framework to do this". Try looking into frameworks, and I'll bet you'll find your answer quite easily...
Have a look at the PRADO framework and the Yii framework. They have a hierarchical, component-based philosphy that allows nesting and composition, essentialling building a whole site from smaller, pluggable components.
As has already been pointed out, PHP is a language, while .Net is a framework. The two are very different things.
If you want to use PHP as a scripting language for .Net, there's always Phalanger

Looking for ideas on a computer science course project [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Hey. I'm taking a course titled Principles of Programming Languages, and I need to decide on a project to do this summer. Here is a short version of what the project needs to accomplish:
"The nature of the project is language processing. Writing a Scheme/Lisp processor is a project of this type. A compiler for a language like C or Pascal is also a potential project of this type. Some past students have done projects related to databases and processing SQL. Another possible project might relate to pattern matching and manipulating XML. Lisp, Pascal, and C usually result in the most straight forward projects."
I am very interested in web technologies, and have some experience with PHP, MySql, JavaScript, etc. and I would like to do something web oriented, but I'm having trouble coming up with any ideas. I also want this to be a worthwhile project that could have some significance, instead of just doing the same thing as everyone else in class.
Any ideas? Thanks!
EDIT: I really like the idea of a Latex to XHTML/MathML translator, and I passed the idea to my instructor, in which he wrote back:
"I think the idea is interesting, my question (and yours) is whether it is appropriate.
I think of LateX as a low-level mark-up language. I'm wondering if converting this to XHTML or MathML is really a change in levels and complexity. I think you can make your point with a little more discussion and some examples. You might also think of some other mark-up constructs which made it easier to describe equations."
Any ideas on how to convince him this may be appropriate, or any extensions of this idea that could work for the goals of my project?
Thanks for all the responses so far!
Hm, neat! Maybe:
1. A web-based language interpreter. eg, a very simple assembly interpreter in javascript, or a PHP-based C interpreter (PHP script reads C code, and executes it in some sort of sandboxed kind of way. Obviously it would only be able to implement a small subset of the C language)
2. Maybe some automated way to transform PHP data structures (like PHP arrays) into SQL queries, and vice versa. That kind of stuff has already been done, but you might be able to do something which (for example) takes an SQL query and creates the array datastructure that would be needed to "hold" the information returned by the SQL. It could support complex things like JOINS and GROUP BYs.
3. Maybe a C-to-PHP compiler? (or a PHP-to-C compiler, to be able to run simple PHP code natively. Use this with any combination of languages)
edit:
4. Maybe a regex-to-C parser. That is, something that takes a regex, and generates C code to match that pattern. Or something which takes a regex, and converts it into an FSM which represents the "mathematical" translation of that expression. Or the opposite - something which takes an FSM for a CFL and generates the perl-syntax regex for it.
5. Maybe an XML-to-PHP/MySQL parser. eg, an XML file might contain information about a database and fields, and then your program creates the SQL to create those tables, or the HTML/PHP code for the forms.
Best of luck!
I'd stay away from PHP and MySQL for a project like this. Both are commercial platforms that have compromised a lot of core CS principles in order to gain market share and solve user's problems. Given what you've described it sounds like the point of this project is to think about how programming languages are processed. Javascript The Language (not the browser API) might be a good choice here. Writing a processor/interpreter/compiler for Javascript or using Javascript itself to write a processor/interpreter/compiler for another language would meet the criteria for the assignment. Writing a Javascript "minifier" that removes all unnecessary white space (for smaller file sizes) while maintaining the program's functionality is another possible project.
Here's something I'd love: a PHP-based LaTeX-to-MathML translator. It wouldn't have to do everything, but if I could just cut-and-paste mathematical formulas written in valid LaTeX code into a window and have the script parse it and convert it into valid MathML, that'd be awesome.
Let me expand on this some more. The current state of scientific publishing on the web isn't great. Titles, headers, section numbers, tables, etc. can all be done in HTML, but for mathematical and chemical formulas which depend on precise two-dimensional formatting, scientific authors have only second-class options:
Publish their work in pdf format, which looks great but has a (comparably) huge file size and doesn't do hyperlinking well, or
Use something like latex-to-html, which converts formulas into .gif files (or some similar image file), which are semantically meaningless and thus doesn't lend themselves to indexing or searching.
Moreover, neither of these options allow for mathematical formulas to be generated programmatically, which would be helpful to the education community (think randomly-generated online homework).
Publishing scientific work in MathML would solve all of these issues, but it has a few of issues of its own, namely:
It's really too verbose to code by hand. I mean, you can do it, but c'mon.
The scientific community uses LaTeX for publishing, they're happy with it (for good reason), and they're not about to learn another mathematical markup language when they've got their own research and lesson-planning to do.
Browser support for MathML is currently pretty limited. I know this, and I don't mean to stick my head in the sand about it.
In other words: scientific authors know LaTeX, they use it daily, it's the de facto standard for authoring scientific content. MathML isn't and won't ever be the way math and science is authored, but it's the only semantically rich way to put hypertext mathematics on the web. Browser support for MathML is weak because nobody uses it; nobody uses it because it's too hard to write by hand. Now, maybe this is wishful thinking, but I have to believe that if it were only easier to write MathML, more scientists and mathematicians, especially the early-adopter types, would at least try it, and this would inspire browsers (especially open-source browsers) to improve their support, which would then lead to more authors using it, etc.
Here's where the translator comes in: Until the barrier-to-entry for MathML drops, it'll never be widely adopted. A simple LaTeX-to-MathML converter would take care of that. It would reduce the barrier-to-entry for MathML to near zero. If it leads to widespread use of and better support for MathML, it would be a major benefit to the scientific and education communities.
I finished this course last semester :)
IMHO the best way to go is to build an expression evaluator. build the simplest expression evaluator you can.
Then add these features in order as many as you like:
1- constant symbols, just place holders for variables. your evaluator should ask for their values after parsing the expression.
2- imperative-style variables. Like variables in any imperative language, where the user can change the value of a symbol anywhere in code.
3- simple control-statements. 'if-else' and pretest while loop are the simplest to consider.
4- arrays. if you really want your expression evaluator to be really like a programming language. It would be interesting if you add variable dimension arrays to your 'language'. you have to build a generic mapping function for your arrays.
Now you got a real programming language. To be a useful one, you might add subroutines.
so the list continuous:
5- subroutines. This is little harder than previous features, but it should not be impossible :)
6- build a simple math library for your new language in your language it self! and that is the fun part in my opinion ;)
Sebest book is a good book to overview famous imperative programming languages.
You shouldn't view creating an implementation of a particular language as insignificant. Everyone probably wants to be a famous programmer and not many people achieve it. This is a great opportunity to be familiar with very cool uncommon languages. (Lisp, APL, etc) If this is your first time creating a compiler/interpreter then it will also be a better choice to go with an already existent language (so you can see what design elements are needed to create a successful language.)
Significant ideas typically arise from necessity. People began using a language because they either needed it or it was a lot easier to accomplish the task they wanted to do. I don't think you will find the answer or the motivation to start a project from scratch here. That being said, I've always thought it would be cool to have a language that uses processor native byte code to create dynamic websites (without using something like cgi).
In response to your edit, here are some latex ideas:
LaTeX-to-ASCII pretty print, perhaps just for a small subset of TeX
LaTeX-to-Maple/Mathcad/Mathematica script, so that equations can be imported or edited or solved (don't know if that already exists)
Javascript LaTeX translator. basically, as you type, it does a translation from latex to html/css/.gif/whatever, so you can see your math "live" as you type it, kinda like the stackoverflow text editor.
Perhaps some sort of latex macros for expressing C code or something? Or how about this: often, C code is doing math: "det = (b*b - 4*a*c); det_sqrt = sqrt(det); etc" How about something which takes C (or java or whatever) code, which is performing a series of arithmetic assignments, and converts it into a nicely-formatted latex list of equations that are human-readable (ie, a \begin{eqnarray} block)
Or something that does the opposite: take a listing of latex computations or equations, and generates C code which declares the requisite variables, gets requisite user input, and performs the computations listed in your latex?
Why not write some sort of interface that can be interpreted/compiled down to the appropriate web technology of the users choice?
Or something like a Python to C compiler?
Just something I thought of recently: write a Ruby interpreter in Lisp.
Something that can be interesting to work on, is a regexp to automaton using Glouchkov's algorithm, here are some key features that can be implemented
Syntaxical analysis of regexp
Transformation into an automaton using Glouchkov's algorithm
Generating random phrases matching the regexp with that automaton / Validating phrases
Exporting automatons using XML
That's not a very long assignment so you may be able to handle it in a few months
You can try to make a scripting language in the vein of nadvsh if you want to do something interesting, but it might be too removed from what your instructor is expecting of you.
New Adventure Shell (nadvsh)
If you want to process language you can do a UIMA program. UIMA stands for Unstructured Information Management Architecture, it was developped by IBM at a cost of about 45Million dollars and is now available opensource. Basically UIMA is ascii codecs to analyse text documents to find patterns. It is made to find things where there is no order(finding needles in hay stacks). It uses XML and C.
The web is a rich area for doing work with languages. Take a look at a popular web framework like Ruby on Rails, and you'll find that much of its productivity comes from the fact that it implements a domain specific language well suited to web applications. Ruby just so happened to be a good language to implement such a language because of its dynamic nature, but the power comes from the language they created from it.
In your case, perhaps you could try designing your own domain specific language using a language that you are familiar with, such as PHP, to implement the essential core of a web framework:
routing URLs to pages
generating pages dynamically using a template (and maybe implement your own template syntax!)
connecting objects to underlying databases (object relational mapping)
If you are really ambitious, instead of building from an existing language, you could build your own language from the ground up (lexer, parser, code generator, etc) to do this.
You can ideas from this massive list.
Writing compiler for C or Pascal will likely take you months or years, if you are not compiler guru.
Write a simple web server. It will be fun and might prove useful as a simple and free solution. I once met a guy who said he did something like this and used for simple customer sites. Yours could become a useful thing as well.

internationalization of php website

I am currently working on a project / website and I will need to make it available in several languages. The site was done with PHP / mysql and a lot of javascript (jQuery). I have no idea where to start and I was hoping somebody could give me some hints. I would like to know opinions about what is the best approach to take, if there are some good tools for such a php site, what to do with the existing scripts, or better, with the text inside of the scripts that need to be translated as well. Does anybody had to do something like this before that could guide me through the right path :) ??
thanks
There are a number of ways of tackling this. None of them "the best way" and all of them with problems in the short term or the long term. The very first thing to say is that multi lingual sites are not easy, translators and lovely people but hard to work with and most programmers see the problem as a technical one only. There is also another dimension, outside the scope of this answer, as to whether you are translating or localising. This involves looking at the target audiences cultural mores and then tailoring language, style, layout, colour, typeface etc., to that culture. Finally do not use MT, Machine Translation, for anything serious or if it needs to be accurate and when acquiring translators ensure that they are translating from a foreign language into their native language which means that they understand all the nuances of the target language.
Right. Solutions. On the basis that you do not want to rewrite the site then simply clone the site you have and translate the copies to the target language. Assuming the code base is stable you can use a VCS to manage any code changes. You can tweak individual parts of the site to fit the target language, for example French text is on average 30% larger than the equivalent English text so using one site to deliver this means you may (will) have formatting problems and need to swap a different css file in and out depending on the language. It might seem a clunky way to do it but then how long are the sites going to exist? The management overhead of doing it this way may well be less than other options.
Second way without rebuilding. Replace all content in the current site with tags and then put the different language in file or db tables, sniff the users desired language (do you have registered users who can make a preference or do you want to get the browser language tag, or is it going to be URL dot-com dot-fr, dot-de that make the choice) and then replace the tags with the target language. Then you need to address the sizing issues and the image issues separately. This solution is in effect when frameworks like Symfony and Zend do to implement l10n.
Then you could rebuild with a framework or with gettext and and possibly have a cleaner solution but remember frameworks were designed to solve other problems, not translation and the translation component has come into the framework as partial solution not the full one.
The big problem with all the solutions is ongoing maintenance. Because not not only do you have a code base but also multiple language bases to maintain. Unless you all in one solution is really clever and effective then to ongoing task will be difficult.

Categories