I just inherited a website written in PHP to internationalize it... The problem is the code is not consistent. It has some functions that have strings inside echo and another pieces and in another functions the php is closed and html is presented the right way.
I was looking for a tool that could easy my job as much as possible. Retrieving as much strings as it could and defining (through defines) in another file.
I though about creating a script with regex functions to achieve this but if there was anything out there... I looked but couldn't find. Maybe I'm using wrong terms as I'm not English native.
Does anyone know a good way to do this?
If you are open to using gettext for I18N, then you can use xgettext: http://www.gnu.org/s/hello/manual/gettext/xgettext-Invocation.html
Here is a tutorial detailing how you can use it in PHP: http://www.phpdig.net/ref/rn26.html
Related
I did some search about localization files, see that we can use .po file or gettext but, is there any tutorial or sample of a unique solution that will work both in javascript and in php.
I want to only maintain one localization file per language that will work with both JS and PHP languages.
Would appreciates if someone can point me to some links or samples...
I found that it is typically a sign of a questionable design when translatable text is coded inside JS functions. JS is meant to implement logic, not content. The content should be provided by PHP (typically by using a templating engine) and should be used by JS. That way you only need a localization solution for PHP.
If (exceptions always occur) you really need to translate a phrase inside a JS routine you use an ajax call to fetch the translation. This also simplifies the access to the dictionary holding the translation tokens since it is again done by PHP. The dictionary can be kept in a single place.
Yep, there is. I've successfully used gettext.js a while ago, which is operating on .json or .po files. This way, you only have to maintain one translation source. The webpage I've used this for is located here so you can check out how I've did it. Good luck!
First, try to avoid gettext if you can. It's popular and stable, but it puts some burden on the translations maintenance: you will need to change the source strings, and when this happens, gettext needs to update all the message keys, and this may mess up the existing translations. An approach with constant message keys is much easier to maintain in the long run - you will need to remember to delete the keys you don't use any more, but it's a very small burden.
You can use the same translations storage format for PHP and JavaScript. If you use a key-based approach, as I suggest, it will probably be some JSON-based format. JSON is easily accessible in both PHP and JavaScript.
There are several ready-made JavaScript libraries for JSON-based internationalization. I happen to be a developer of one such library: https://github.com/wikimedia/jquery.i18n . It should be reasonably easy to adapt it to PHP.
I know this is possibly an obscure use for php, but I'm working on an idea to navigate the human genome in a rather interesting way.
The problem is I need to know if I can write a php script to parse the freely available data, and if so how would I start? Are there any php scripts to do this in existence?
I'd suggest creating a database design (MySQL) that has the subset of data you want to explore in the PHP application.
Then find a way to upload the data into that data schema. For the uploading part you could use a more poweful language than PHP of your choice, it could be C#, F#, Haskell, or whatever.
This seperation will help simplify things more than doing it all in PHP.
You'll have to write a parser for that, but that should be fairly simple:
http://jc.unternet.net/genome/2bitformat.html
And an example in Perl: http://www.perlmonks.org/?node_id=672251
I'm making a search engine that (in theory) analyzes online encyclopedias to get answers to a user's question from a form. However, I want to know if I'm wasting my time with the PHP. If I am, what language would be best suited to this task? If I'm not, what function in PHP would allow me to do this? Thanks!
PHP works as well as anything else. If you want to read data off of another webpage, you'll probably want to use cURL, which is built in to PHP.
All of the requisite pieces are there: PHP does fine with processing text and HTML. If you already know PHP, it's best to stick with what you know.
This is easy enough to do with PHP. If the sites you are getting the data from are valid xhtml it will be extremely easy to process the page and extract the data using the simplexml extension.
See How to escape quote PHP strings generated by Delphi?
I am just interested to hear if anyone has used Delphi (or possibly BCB) as a code generator for PHP ...
(or thoughts about code generation from one language to another in general)
Hmm, any good books about code generation ?
I've generated javascript, SQL and Delphi many times. But mostly is basic substitution, (and the example in the post you mention looks the same), not really codegeneration in the "compiler" sense of the word.
But there are also many real compilers in Pascals and Delphi like dialects. The biggest one I think is Free Pascal (http://www.freepascal.org), which is a compiler for Object Pascal (aka delphi)
(added later:)
Besides variable substitution, basic templating engines also fall in this category. Templates are sometimes easier maintainable than the same fragement code. Specially in html/cgi land this is used a lot.
You can generate anything from a tool which can export text files no?
You can write all by the hand, or in a "delphi style" by using Delphi for PHP http://www.embarcadero.com/products/delphi-for-php
best regards,
anyone has used Delphi (or possibly BCB) as a code generator for PHP
PHP - no, but I'm generating a lot of Delphi/Pascal code from Delphi. I've also generated all other things used for a web application: HTML, JavaScript, CSS - but never PHP because I didn't need that. So it's possible, but simply knowing it's possible is not going to help you much.
thoughts about code generation from one language to another in general
You need to look into "text template engines" for Delphi. I can't suggest any because I wrote my own (and I'm not planing on releasing my own under any license).
I am using phpTumblr, a wrapper around the tumblr blog api that allows you to access posts via php.
I want the site to display new posts dynamically, so I am using php to write html code. I find myself writing things like print(blablabla); or print(); ... and so on, and setting the header of the document to text/html, so that the browser would read it as html.
This just seems to me like a kind of ugly hack, and I was wondering if most dynamic pages are set up in this way, or are there different ways to convert php objects(say arrays) automatically into html tags. So far it doesnt seem like there are any.) maybe i have to be using some CMS software?
Any advice would be great.
Thanks
I believe what you're describing is known as a template engine. It essentially separates the logic from the UI, and allows you to write dynamic pages without an excessive number of print or echo statements.
For PHP, I would recommend Smarty, but Google can also help you with finding alternative ones if you find you don't like it.
PHP is a language in which you can do alot of different things and one of them is to send output to browsers. So if you want to print an array as HTML code , write a PHP function for it. PHP has NOTHING to do with HTML tags directly.
Like the above post mentions you can use Smarty templating engine ... BUT then you will need to learn the smarty language to print the array :)
All scripting languages work in this way. So lets say if any xyz language supports a function called print_array_as_html($array) .... then observe that it is a function. That's the idea of having functions/methods in a language , extend the functionality to get what you need.