Website Language translation in php - php

Hi i am devloping site in php i need to translate whole website in other language like german,spanish etc... how can it possible in php i have tried using some csv but it all goes static i mean i can not convert the whole website ..
if you have any csv or api information ..
please help..
-Div

<?php
$GLOBAL['langs']=array(
'en'=>array(
'Welcome to my site!'=>'Welcome to my site!'
),
'it'=>array(
'Welcome to my site!'=>'Benvenuto sul mio sito web!'
)
);
function _($text){
$lang=$_COOKIE['lang'];
return $GLOBAL['langs'][$text];
}
?><html><head><?php
echo '<title>'._('Welcome to my site!').'</title>';
?></head><body>
....
</body></html>

While Christian's answer will do the trick there's a more cleaner and efficient way to achieve your needs: gettext is PHP's built-in function for internationalization.

Zend Translate is a library exactly build for this.
Gettext is not thread-safe. PHP supports only gettext and native array. Zend_Translate supports several source formats, including those supported by PHP, and other formats including TMX and CSV files.

If you want to translate 'a whole page' to any language, you could use Google Translate.

Related

Using Drupal, how do I force t('') to populate the string?

I am trying to make sure that all of the translatable strings are present in the database. Some of them appear very rarely (various form validation errors), therefore it would be a pain to reproduce them all.
Instead, I've created an admin module that, once called, goes through an array of all translatable strings and executes echo t('[the string from the array]').
After this, I expect to be able to translate those strings using admin/config/regional/translate/translate. But not all of them are there.
What am I missing?
If that's for some reason not possible, is there any function that would force entry?
Install and use "Translation template extractor" module as suggested by Vlad Stratulat.
The module let you parse all of your modules and themes and extract all the strings that are used in t() function.
The result is a .po file with all the original/translation string pairs.
You can open the .po (it's a plain text file, so use your favorite text-editor or POEdit software) and check and translate the missing strings.
Finally re-upload the complete .po in Drupal.
Try the following.
foreach(array(t('foo'), t('bar')) as $t) {
echo $t;
}
You should never use t() to translate variables, such as calling t($text). Read about t() function.
But anyway, the best way is to create .po file from your module and export it to Drupal using Translation template extractor.
This module will extract all your translatable strings and will save it into language specific file which you can then use in any other sites where you would like to use your module.

Read the content of a PDF with PHP?

I need to read certain parts from a complex PDF. I searched the net and some say FPDF is good, but it cant read PDF, it can only write. Is there a lib out there which allows to get certain content of a given PDF?
If not, whats a good way to read certain parts of a given PDF?
Thanks!
I see two solutions here:
converting your PDF file into something else before: text, html.
using a library to do so and bad news here, most of them are written in Java.
https://whatisprymas.wordpress.com/2010/04/28/lucene-how-to-index-pdf-files/
What about that ?
http://www.phpclasses.org/package/702-PHP-Searches-pdf-documents-for-text.html
ps: I don't test this class, just read the description.
$result = pdf2text ('sample.pdf');
echo "<pre>$result</pre>";
How to get “clean” text :source code pdf2text
http://webcheatsheet.com/php/reading_clean_text_from_pdf.php

Parsing Wiki API content

I have this wiki from the API http://fr.wikipedia.org/w/api.php?action=query&titles=%C9rythropo%EF%E9tine&prop=revisions&rvprop=content&format=xmlfm
which I would like to retrieve the main content starting from:
L''''érythropoïétine''' ('''EPO''') est une [[hormone]] ......etc
I tried for a start to preg_replace everything from the top starting from the word "{{Chimiebox..." to the bottom "}}" using this
preg_replace( '/^{{(.*)}}$/sim', '', $value[0]['*'] );
But kind of doesn't work..does anyone know of a good way to determine the start of the content?? Thanks for any advice.
Well, afaik the most projects use the Wikipedia Parser directly, e.g. the Wikipedia Offline Client Project at my university. Since you seem to be using php, this may the be the easiest way for you.

Am I breaking any "php good practice" in the following php array which deals with 3 (human) languages?

This is the most optimal way of dealing with a multilingual website I can think of, right now (not sure) which doesn't involve gettext, zend_translate or any php plugin or framework.
I think its pretty straight forward: I have 3 languages and I write their "content" in different files (in form of arrays), and later, I call that content to my index.php like you can appreciate in the following picture:
alt text http://img31.imageshack.us/img31/1471/codew.png
I just started with php and I would like to know if I'm breaking php good practices, if the code is vulnerable to XSS attack or if I'm writing more code than necessary.
EDIT: I posted a picture so that you can see the files tree (I'm not being lazy)
EDIT2: I'm using Vim with the theme ir_black and NERDTree.
Looks all right to me, although I personally prefer creating and using a dictionary helper function:
<?php echo dictionary("showcase_li2"); ?>
that would enable you to easily switch methods later, and gives you generally more control over your dictionary. Also with an array, you will have the problem of scope - you will have to import it into every function using global $language; very annoying.
You will probably also reach the point when you have to insert values into an internationalized string:
You have %1 votes left in the next %2 hours.
Sie haben %1 stimmen übrig für die nächsten %2 stunden.
Sinulla on %1 ääntä jäljellä seuraavan %2 tunnin ajassa.
that is something a helper function can be very useful for:
<?php echo dictionary("xyz", $value1, $value2 ); ?>
$value1 and $value2 would be inserted into %1 and %2 in the dictionary string.
Such a helper function can easily be built with an unlimited number of parameters using func_get_args().
It's OK generally. For instance, punBB's localization works this way. It is very fast. Faster than calling a function or an object's method or property. But I see a problem with this approach, since it doesn't support language fallbacks easily. I mean, if you don't have a string for Chinese, let it be displayed in English.
This problem is topical when you upgrade your system and you don't have time to translate everything in every language.
I'd better use something like
lang.en.php
$langs['en'] = array(
...
);
lang.cn.php
$langs['cn'] = array(
...
);
[prepend].php (some common lib)
define('DEFAULT_LANG', 'en');
include_once('lang.' . DEFAULT_LANG '.php');
include_once('lang.' . $user->lang . '.php');
$lang = array_merge($langs[DEFAULT_LANG], $langs[$user->lang]);
Looks all right to me also, but:
Seems that you have localization for multiple modules/sites, so why not break it down to multidimensional array?
$localization = array(
'module' => (object)array(
'heading' => 'oh, no!',
'perex' => 'oh, yes!'
)
);
I personally like to creat stdClass out of arrays with
$localization = (object)$localization;
so you can use
$localization->module->heading;
:) my 2 cents
The only way that this could be xss is if you have register_globals=On and you don't set $lang['showcase_lil'] or other $lang's. But I don't think you have to worry about this. So I think your in the clear.
as an xss test:
http://127.0.0.1/whatever.php?lang[showcase_lil]=alert(/xss/)
Wouldn't it have been better to post code and briefly explain this issue to us?
Anyway, putting each language in its own file and loading it through some sort of language component seems okay. I'd prefer using some sort of gettext, but this is okay too, I guess.
You should make a function for calling the language keys rather than relying on an array, something like
<?php echo lang('yourKey'); ?>
One thing to watch for is interpolation; that's really the only place XSS could sneak in if your server settings are sensible. If you at any point need to do something along the lines of translating "$project->name has $project->member_count members", you'll have to make sure you escape all HTML that goes in there.
But other than that, you should be fine.

rtf format to pdf

Is there any way to convert rtf format to pdf using PHP?
Thanks
If you want to stick with pure PHP, you can probably use HTML as an intermediary:
Convert RTF to HTML
http://freshmeat.net/projects/rtf2htm/ , http://www.phpclasses.org/package/1930-PHP-RTF-to-HTML-converter-with-latin-character-support.html
Optionally: clean up the HTML
http://htmlpurifier.org/
Convert HTML to PDF
http://dompdf.github.io/
You can use OpenOffice command line interface for that. Check my answer to a similar question.
Ted is the tool you're looking for. Ted brings also a script called rtf2pdf.sh you can execute by PHP to create a PDF file.
You should try out livedocx livedocx.com . The latest Zend Framework 1.10 has a ready built module to help you out. You can read more about it at this place http://www.phpfreaks.com/tutorial/template-based-document-generation-using-livedocx-and-zend-framework

Categories