I have a script I'm using to convert English text to other spoken languages, and I have a php code handling it like so.
ini_set("max_execution_time", "300");
header('Content-Type: text/html; charset=utf-8');
require_once('googleTranslate.class.php');
$gt = new Google_Translate_API();
$title = $gt->translate($title, "en", $lang);
$keywords = $gt->translate($keywords, "en", $lang);
$body = $gt->translate($body, "en", $lang);
Anyways, when I run this script, it times out before it can translate much text at all, so I was wondering if we could run one piece at a time somehow?
Also, note that the googleTranslate.class.php can be freely downloaded from Google Code.
Cheers
Hm, well I think you shoulde cache results at first. Make some script witch caches translated content for needed languages. Else you fast will reach querys limit.
Got it. Turns out Google Translate API wasn't the way to go. Instead, I'm using the Bing Translate service. It also doesn't restrict me to certain lengths of text. Thanks for the help. (:
Related
I have my php gettext default language in English let's say
I would like in one of my controller, to translate some words in 2 other languages and put them all in an array.
ideally I could do
$word_sv = gettext($word, 'sv_SV');
$word_fi = gettext($word, 'fi_FI');
but it doesn't exist.
Is the only way to change the overall gettext settings each time?
function setLang($lang){
putenv("LC_ALL=$lang");
setlocale(LC_ALL, $lang);
bindtextdomain("myPHPApp", "./locale");
textdomain("myPHPApp");
}
setLang('sv_SV');
$word_sv = gettext($word);
setLang('fi_FI');
$word_fi = gettext($word);
related: saw it on Google after : i18n with gettext but without the locale hassle?
Edit
here are the proposed answered solutions:
https://github.com/Philipp15b/php-i18n (seems best solution)
http://glotpress.trac.wordpress.org/browser/trunk/pomo (could use it if I find a good doc or tuto ;))
change locale on the fly, probably not good
I know the pains of using gettext, but its performance is what keeps me with it !
In your case, you might want to look at this little project ? i'm pretty sure this might help you !
this simply uses .ini files with translations, you can freely switch between files and echo the different languages for the same word.
https://github.com/Philipp15b/php-i18n
If you are bound to gettext here, Let the computer do the work for you.
You either have a list of words you then want to check against all languages, the do the wordlist for each language first. That will spare you some overhead to call the setlanguage function between each word and language.
If you want to go each language, each word, write the functions that way:
function gettext_by_lang($lang, $word) {
putenv("LC_ALL=$lang");
setlocale(LC_ALL, $lang);
bindtextdomain("myPHPApp", "./locale");
textdomain("myPHPApp");
return gettext($word);
}
$word_sv = gettext_by_lang('sv_SV', $word);
$word_fi = gettext_by_lang('fi_FI', $word);
This would at least make your code more compact. Another idea that comes to mind is using a a parser for PO and MO files so that you can check for the data.
In PHP one of that is shipping with Wordpress / Glotpress:
http://glotpress.trac.wordpress.org/browser/trunk/pomo
Maybe this helps. That library is being maintained.
I guess an obvious answer is to roll your own global function:
function getLocalText($string, $lang)
{
putenv("LC_ALL=$lang");
setlocale(LC_ALL, $lang);
bindtextdomain("myPHPApp", "./locale");
textdomain("myPHPApp");
return gettext($string);
}
$word_fi = getLocalText($word, 'fi_FI');
How to convert something like this
\xe6\xa6\x82\xe8\xa6\x81\n\xe3\x83\xbb\xe3\x82\xb0\xe3\x83\xaa\xe3\x83\xbc\xe3\x81\xae\xe3\x82\xa8\xe3\x83\xb3\xe3\x82\xb8\xe3\x83\x8b\xe3\x82\xa2\xe3\x81\xab\xe5\xbf\x9c\xe5\x8b\x9f\xe3\x81\x97\xe3\x81\xa6\xe3\x81\xbf\xe3\x81\x9f\xe3\x81\x84\xe3\x81\x8c\xe3\x80\x81\xe5\xbf\x9c\xe5\x8b\x9f\xe5\x89\x8d\xe3\x81\xab\xe8\x87\xaa\xe5\x88\x86\xe3\x81\xae\xe5\xae\x9f\xe5\x8a\x9b\xe3\x82\x92\xe8\xa9\xa6\xe3\x81\x97\xe3\x81\xa6\xe3\x81\xbf\xe3\x81\x9f\xe3\x81\x84\xe3\x80\x82\n\xe3\x83\xbb\xe5\x9c\xb0\xe6\x96\xb9\xe3\x81\xab\xe4\xbd\x8f\xe3\x82\x93\xe3\x81\xa7\xe3\x81\x84\xe3\x82\x8b\xe3\x81\xae\xe3\x81\xa7\xe9\x9d\xa2\xe6\x8e\xa5\xe5\x9b\x9e\xe6\x95\xb0\xe3\x81\x8c\xe5\xb0\x91\xe3\x81\xaa\xe3\x81\x84\xe6\x96\xb9\xe3\x81\x8c\xe3\x81\x82\xe3\x82\x8a\xe3\x81\x8c\xe3\x81\x9f\xe3\x81\x84\xe3\x80\x82\n\xe3\x83\xbb\xe9\x9d\xa2\xe6\x8e\xa5\xe3\x81\xaf\xe8\x8b\xa6\xe6\x89\x8b\xe3\x81\xa0\xe3\x81\x8c\xe3\x83\x97\xe3\x83\xad\xe3\x82\xb0\xe3\x83\xa9\xe3\x83\x9f\xe3\x83\xb3\xe3\x82\xb0\xe3\x81\xab\xe3\x81\xaf\xe8\x87\xaa\xe4\xbf\xa1\xe3\x81\x8c\xe3\x81\x82\xe3\x82\x8b\xe3\x80\x82\xe3\x81\xaf\xe3\x80\x81\xe3\x81\x93\xe3\x81\xae\xe3\x82\x88\xe3\x81\x86\xe3\x81\xaa\xe6\x96\xb9\xe3\x80\x85\xe3\x81\xae\xe3\x81\x94\xe8\xa6\x81\xe6\x9c\x9b\xe3\x81\xab\xe3\x81\x8a\xe5\xbf\x9c\xe3\x81\x88\xe3\x81\x99\xe3\x82\x8b\xe3\x81\x9f\xe3\x82\x81\xe3\x81\xab\xe4\xbd\x9c\xe3\x82\x89\xe3\x82\x8c\xe3\x81\x9f\xe6\x96\xb0\xe3\x81\x97\xe3\x81\x84\xe6\x8e\xa1\xe7\x94\xa8\xe3\x83\x97\xe3\x83\xad\xe3\x82\xb0\xe3\x83\xa9\xe3\x83\xa0\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82\n\xe3\x83\x97\xe3\x83\xad\xe3\x82\xb0\xe3\x83\xa9\xe3\x83\x9f\xe3\x83\xb3\xe3\x82\xb0\xe3\x82\xb9\xe3\x82\xad\xe3\x83\xab\xe3\x82\x92\xe8\xa9\x95\xe4\xbe\xa1\xe3\x81\x99\xe3\x82\x8b\xef\xbc\x91\xe6\xac\xa1\xe9\x9d\xa2\xe6\x8e\xa5\xe3\x82\x92\xe3\x83\x91\xe3\x82\xb9\xe3\x81\xa7\xe3\x81\x8d\xe3\x81\xbe\xe3\x81\x99\xe3\x81\xae\xe3\x81\xa7\xe5\x8a\xb9\xe7\x8e\x87\xe7\x9a\x84\xe3\x81\xaa\xe8\xbb\xa2\xe8\x81\xb7\xe6\xb4\xbb\xe5\x8b\x95\xe3\x82\x92\xe8\xa1\x8c\xe3\x81\xa3\xe3\x81\xa6\xe9\xa0\x82\xe3\x81\x91\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\n\xe3\x82\x82\xe3\x81\xa1\xe3\x82\x8d\xe3\x82\x93\xe5\xad\xa6\xe7\x94\x9f\xe3\x81\xae\xe7\x9a\x86\xe3\x81\x95\xe3\x82\x93\xe3\x81\xae\xe3\x83\x81\xe3\x83\xa3\xe3\x83\xac\xe3\x83\xb3\xe3\x82\xb8\xe3\x82\x82\xe3\x81\x8a\xe5\xbe\x85\xe3\x81\xa1\xe3\x81\x97\xe3\x81\xa6\xe3\x81\x8a\xe3\x82\x8a\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82
which i received in HTTP POST to show them properly on HTML web page.
I have no idea what I am looking at but i think i can be converted to something which look in this ☺ format.
How can i do this in PHP
If you send the appropriate character set encoding with your HTTP response, you don't have to do anything to the data, the browser should properly decode it as Japanese text.
Example:
<?php
header('Content-Type: text/html; charset=UTF-8');
$var = "\xe6\xa6\x82\xe8\xa6\x81\n\xe3\x83\xbb\xe3\x82\xb0\xe3\x83\xaa\xe3\x83\xbc\xe3\x81\xae\xe3\x82\xa8\xe3\x83\xb3\xe3\x82\xb8\xe3\x83\x8b\xe3\x82\xa2\xe3\x81\xab\xe5\xbf\x9c\xe5\x8b\x9f\xe3\x81\x97\xe3\x81\xa6\xe3\x81\xbf\xe3\x81\x9f\xe3\x81\x84\xe3\x81\x8c\xe3\x80\x81\xe5\xbf\x9c\xe5\x8b\x9f\xe5\x89\x8d\xe3\x81\xab\xe8\x87\xaa\xe5\x88\x86\xe3\x81\xae\xe5\xae\x9f\xe5\x8a\x9b\xe3\x82\x92\xe8\xa9\xa6\xe3\x81\x97\xe3\x81\xa6\xe3\x81\xbf\xe3\x81\x9f\xe3\x81\x84\xe3\x80\x82\n\xe3\x83\xbb\xe5\x9c\xb0\xe6\x96\xb9\xe3\x81\xab\xe4\xbd\x8f\xe3\x82\x93\xe3\x81\xa7\xe3\x81\x84\xe3\x82\x8b\xe3\x81\xae\xe3\x81\xa7\xe9\x9d\xa2\xe6\x8e\xa5\xe5\x9b\x9e\xe6\x95\xb0\xe3\x81\x8c\xe5\xb0\x91\xe3\x81\xaa\xe3\x81\x84\xe6\x96\xb9\xe3\x81\x8c\xe3\x81\x82\xe3\x82\x8a\xe3\x81\x8c\xe3\x81\x9f\xe3\x81\x84\xe3\x80\x82\n\xe3\x83\xbb\xe9\x9d\xa2\xe6\x8e\xa5\xe3\x81\xaf\xe8\x8b\xa6\xe6\x89\x8b\xe3\x81\xa0\xe3\x81\x8c\xe3\x83\x97\xe3\x83\xad\xe3\x82\xb0\xe3\x83\xa9\xe3\x83\x9f\xe3\x83\xb3\xe3\x82\xb0\xe3\x81\xab\xe3\x81\xaf\xe8\x87\xaa\xe4\xbf\xa1\xe3\x81\x8c\xe3\x81\x82\xe3\x82\x8b\xe3\x80\x82\xe3\x81\xaf\xe3\x80\x81\xe3\x81\x93\xe3\x81\xae\xe3\x82\x88\xe3\x81\x86\xe3\x81\xaa\xe6\x96\xb9\xe3\x80\x85\xe3\x81\xae\xe3\x81\x94\xe8\xa6\x81\xe6\x9c\x9b\xe3\x81\xab\xe3\x81\x8a\xe5\xbf\x9c\xe3\x81\x88\xe3\x81\x99\xe3\x82\x8b\xe3\x81\x9f\xe3\x82\x81\xe3\x81\xab\xe4\xbd\x9c\xe3\x82\x89\xe3\x82\x8c\xe3\x81\x9f\xe6\x96\xb0\xe3\x81\x97\xe3\x81\x84\xe6\x8e\xa1\xe7\x94\xa8\xe3\x83\x97\xe3\x83\xad\xe3\x82\xb0\xe3\x83\xa9\xe3\x83\xa0\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82\n\xe3\x83\x97\xe3\x83\xad\xe3\x82\xb0\xe3\x83\xa9\xe3\x83\x9f\xe3\x83\xb3\xe3\x82\xb0\xe3\x82\xb9\xe3\x82\xad\xe3\x83\xab\xe3\x82\x92\xe8\xa9\x95\xe4\xbe\xa1\xe3\x81\x99\xe3\x82\x8b\xef\xbc\x91\xe6\xac\xa1\xe9\x9d\xa2\xe6\x8e\xa5\xe3\x82\x92\xe3\x83\x91\xe3\x82\xb9\xe3\x81\xa7\xe3\x81\x8d\xe3\x81\xbe\xe3\x81\x99\xe3\x81\xae\xe3\x81\xa7\xe5\x8a\xb9\xe7\x8e\x87\xe7\x9a\x84\xe3\x81\xaa\xe8\xbb\xa2\xe8\x81\xb7\xe6\xb4\xbb\xe5\x8b\x95\xe3\x82\x92\xe8\xa1\x8c\xe3\x81\xa3\xe3\x81\xa6\xe9\xa0\x82\xe3\x81\x91\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\n\xe3\x82\x82\xe3\x81\xa1\xe3\x82\x8d\xe3\x82\x93\xe5\xad\xa6\xe7\x94\x9f\xe3\x81\xae\xe7\x9a\x86\xe3\x81\x95\xe3\x82\x93\xe3\x81\xae\xe3\x83\x81\xe3\x83\xa3\xe3\x83\xac\xe3\x83\xb3\xe3\x82\xb8\xe3\x82\x82\xe3\x81\x8a\xe5\xbe\x85\xe3\x81\xa1\xe3\x81\x97\xe3\x81\xa6\xe3\x81\x8a\xe3\x82\x8a\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82";
echo $var;
Since we send a header saying that the character encoding is UTF-8, the browser knows to decode it as such. You could also use a meta-tag to specify the charset. If the browser was set to auto-detect the code, neither option is necessary, but you can't rely on that.
It looks like Japan
php > echo "\xe6\xa6\x82\xe8\xa6\x81\n\xe3\x83\xbb\xe3\x82\xb0\xe3\x83\xaa\xe3\x83\xbc\xe3\x81\xae\xe3\x82\xa8\xe3\x83\xb3\xe3\x82\xb8\xe3\x83\x8b\xe3\x82\xa2\xe3\x81\xab\xe5\xbf\x9c\xe5\x8b\x9f\xe3\x81\x97\xe3\x81\xa6\xe3\x81\xbf\xe3\x81\x9f\xe3\x81\x84\xe3\x81\x8c\xe3\x80\x81\xe5\xbf\x9c\xe5\x8b\x9f\xe5\x89\x8d\xe3\x81\xab\xe8\x87\xaa\xe5\x88\x86\xe3\x81\xae\xe5\xae\x9f\xe5\x8a\x9b\xe3\x82\x92\xe8\xa9\xa6\xe3\x81\x97\xe3\x81\xa6\xe3\x81\xbf\xe3\x81\x9f\xe3\x81\x84\xe3\x80\x82\n\xe3\x83\xbb\xe5\x9c\xb0\xe6\x96\xb9\xe3\x81\xab\xe4\xbd\x8f\xe3\x82\x93\xe3\x81\xa7\xe3\x81\x84\xe3\x82\x8b\xe3\x81\xae\xe3\x81\xa7\xe9\x9d\xa2\xe6\x8e\xa5\xe5\x9b\x9e\xe6\x95\xb0\xe3\x81\x8c\xe5\xb0\x91\xe3\x81\xaa\xe3\x81\x84\xe6\x96\xb9\xe3\x81\x8c\xe3\x81\x82\xe3\x82\x8a\xe3\x81\x8c\xe3\x81\x9f\xe3\x81\x84\xe3\x80\x82\n\xe3\x83\xbb\xe9\x9d\xa2\xe6\x8e\xa5\xe3\x81\xaf\xe8\x8b\xa6\xe6\x89\x8b\xe3\x81\xa0\xe3\x81\x8c\xe3\x83\x97\xe3\x83\xad\xe3\x82\xb0\xe3\x83\xa9\xe3\x83\x9f\xe3\x83\xb3\xe3\x82\xb0\xe3\x81\xab\xe3\x81\xaf\xe8\x87\xaa\xe4\xbf\xa1\xe3\x81\x8c\xe3\x81\x82\xe3\x82\x8b\xe3\x80\x82\xe3\x81\xaf\xe3\x80\x81\xe3\x81\x93\xe3\x81\xae\xe3\x82\x88\xe3\x81\x86\xe3\x81\xaa\xe6\x96\xb9\xe3\x80\x85\xe3\x81\xae\xe3\x81\x94\xe8\xa6\x81\xe6\x9c\x9b\xe3\x81\xab\xe3\x81\x8a\xe5\xbf\x9c\xe3\x81\x88\xe3\x81\x99\xe3\x82\x8b\xe3\x81\x9f\xe3\x82\x81\xe3\x81\xab\xe4\xbd\x9c\xe3\x82\x89\xe3\x82\x8c\xe3\x81\x9f\xe6\x96\xb0\xe3\x81\x97\xe3\x81\x84\xe6\x8e\xa1\xe7\x94\xa8\xe3\x83\x97\xe3\x83\xad\xe3\x82\xb0\xe3\x83\xa9\xe3\x83\xa0\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82\n\xe3\x83\x97\xe3\x83\xad\xe3\x82\xb0\xe3\x83\xa9\xe3\x83\x9f\xe3\x83\xb3\xe3\x82\xb0\xe3\x82\xb9\xe3\x82\xad\xe3\x83\xab\xe3\x82\x92\xe8\xa9\x95\xe4\xbe\xa1\xe3\x81\x99\xe3\x82\x8b\xef\xbc\x91\xe6\xac\xa1\xe9\x9d\xa2\xe6\x8e\xa5\xe3\x82\x92\xe3\x83\x91\xe3\x82\xb9\xe3\x81\xa7\xe3\x81\x8d\xe3\x81\xbe\xe3\x81\x99\xe3\x81\xae\xe3\x81\xa7\xe5\x8a\xb9\xe7\x8e\x87\xe7\x9a\x84\xe3\x81\xaa\xe8\xbb\xa2\xe8\x81\xb7\xe6\xb4\xbb\xe5\x8b\x95\xe3\x82\x92\xe8\xa1\x8c\xe3\x81\xa3\xe3\x81\xa6\xe9\xa0\x82\xe3\x81\x91\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\n\xe3\x82\x82\xe3\x81\xa1\xe3\x82\x8d\xe3\x82\x93\xe5\xad\xa6\xe7\x94\x9f\xe3\x81\xae\xe7\x9a\x86\xe3\x81\x95\xe3\x82\x93\xe3\x81\xae\xe3\x83\x81\xe3\x83\xa3\xe3\x83\xac\xe3\x83\xb3\xe3\x82\xb8\xe3\x82\x82\xe3\x81\x8a\xe5\xbe\x85\xe3\x81\xa1\xe3\x81\x97\xe3\x81\xa6\xe3\x81\x8a\xe3\x82\x8a\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82";
概要
・グリーのエンジニアに応募してみたいが、応募前に自分の実力を試してみたい。
・地方に住んでいるので面接回数が少ない方がありがたい。
・面接は苦手だがプログラミングには自信がある。は、このような方々のご要望にお応えするために作られた新しい採用プログラムです。
プログラミングスキルを評価する1次面接をパスできますので効率的な転職活動を行って頂けます。
もちろん学生の皆さんのチャレンジもお待ちしております。
The google translate
Summary
-But I would like to apply for the engineers of the glee, want to try their strength before application.
The smaller the number of times, because they appreciate the interview live in rural areas.
• The programming is confident but not good interview. Is a new adoption program was created to meet the needs of people like this.
You can Tenshoku efficient activities so you can pass the next one interview to evaluate the programming.
We look forward to challenge of course students
Maybe I'm wrong ;)
I have actually no real clue why you get this as POST, but I assume that
\x82
(and the like) stands for a hexa-decimal number. To convert a whole string (ensure it's that format):
$string = eval('return "' . $thatExactInputAsGiven . '";');
$string does now contain the byte-sequence that this submission represents. However I can not tell you which encoding it is in, but probably this one-line above helps you for testing.
If you fear the eval, mind the error handling:
$string = implode('', array_map(function($v){
$r = sscanf($v, '\x%x', $ord);
if (!$r) throw new Exception('Invalid input.');
return chr($ord);
}, str_split($thatExactInputAsGiven, 4)));
I need to be able to permanently change variables in a php file using php.
I am creating a multilanguage site using codeigniter and using the language helper which stores the text in php files in variables in this format:
$lang['title'] = "Stuff";
I've been able to access the plain text of the files using fopen() etc and I it seems that I could probably locate the areas I want to edit with with regular expressions and rewrite the file once I've made the changes but it seems a bit hacky.
Is there any easy way to edit these variables permanently using php?
Cheers
If it's just an array you're dealing with, you may want to consider var_export. It will print out or return the expression in a format that's valid PHP code.
So if you had language_foo.php which contained a bunch of $lang['title'] = "Stuff"; lines, you could do something along the lines of:
include('language_foo.php');
$lang['title2'] = 'stuff2';
$data = '$lang = ' . var_export($lang, true) . ';';
file_put_contents('language_foo.php', '<?PHP ' . $data . ' ?>');
Alternatively, if you won't want to hand-edit them in the future, you should consider storing the data in a different way (such as in a database, or serialize()'d, etc etc).
It looks way easier to store data somewhere else (for instance, a database) and write a simple script to generate the *.php files, with this comment on top:
#
# THIS FILE IS AUTOGENERATED - DO NOT EDIT
#
I once faced a similar issue. I fixed it by simply adding a smarty template. The way I did it was as follows:
Read the array from the file
Add to the array
Pass the array to smarty
Loop over the array in smarty and generate the file using a template (this way you have total control, which might be missing in reg-ex)
Replace the file
Let me know if this helps.
Assuming that
You need the dictionary file in a human-readable and human-editable form (no serializing etc.)
The Dictionary array is an one-dimensional, associative array:
I would
Include() the dictionary file inside a function
Do all necessary operations on the $lang array (add words, remove words, change words)
Write the $lang array back into the file using a simple loop:
foreach ($lang as $key => $value)
fwrite ($file, "\$lang['$key'] = '$value';\n";
this is an extremely limited approach, of course. I would be really interested to see whether there is a genuine "PHP source code parser, changer and writer" around. This should be possible to do using the tokenizer functions.
If it also is about a truly multilingual site, you might enjoy looking into the gettext extension of PHP. It falls back to a library that has been in use for localizing stuff for many years, and where tools to keep up with the translation files have been around for almost quite as long. This makes supporting all the languages in later revisions of the product more fun, too.
In other news, I would not use an array but rather appropriate definitions, so that you have a file
switch ($lang) {
case 'de':
define('HELLO','Hallo.');
define('BYE','Auf wiedersehen.');
break;
case 'fr':
define('HELLO','Bonjour');
define('BYE','Au revoir.');
break;
case 'en':
default:
define ('HELLO','Hello.');
define ('BYE','Bye.');
}
And I'd also auto-generate that from a database, if maintenance becomes a hassle.
Pear Config will let you read and write PHP files containing settings using its 'PHPArray' container. I have found that the generated PHP is more readable than that from var_export()
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.
I know, I know - obfuscated html/js code is useless (I read the other questions on SO), but I still want to make life harder for copy-cats of my site...
I'm running a php based website, which generates html output. I would like the FINAL html output (which has html, js, json and uses ajax) to be obfuscated. Is there a php function for that purpose? I found http://www.ioncube.com/html_encoder.php but that relies on some of their special software to be loaded on the server - ie, a no-go...
Any suggestions?
Not true obfuscation, but rather hard to read in most cases (and less bandwidth-intensive as well!)
<?php
ob_start();
// Generate output here
$output = ob_get_contents();
ob_end_clean();
$output = preg_replace('\s{2,}',' ', $output);
echo $output;
?>
You can compress your JavaScript and css
For php output it can be done using ob_start have a look at this http://ru.php.net/manual/en/function.ob-start.php#71953
You should have a look at Minify it has a Minify_HTML class removing whitespace, unnecessary comments and tokens
Well, in my studies of HTML obfuscator, like http://htmlobfuscator.com/, are truely change their "special" code into reversed base64.
When we decode it, they're actually packed js file using packer that you could find on Google.
So, now we could do this
Slashup the whole html, for the Js string, then "pack" the javascript, then encode it into base64, then rotate the encoded string. Viola, done.
You'll get something like this:
var IO1='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';function l1O(data){var OOOlOI="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";var o1,o2,o3,h1,h2,h3,h4,bits,i=0,enc='';do{h1=OOOlOI.indexOf(data.charAt(i++));h2=OOOlOI.indexOf(data.charAt(i++));h3=OOOlOI.indexOf(data.charAt(i++));h4=OOOlOI.indexOf(data.charAt(i++));bits=h1>16&0xff;o2=bits>>8&0xff;o3=bits&0xff;if(h3==64){enc+=String.fromCharCode(o1)}else if(h4==64){enc+=String.fromCharCode(o1,o2)}else{enc+=String.fromCharCode(o1,o2,o3)}}while(i= 0; i-- ){ ret += string.charAt(i);} return ret; }eval(l1O(OOO(IO1)));
Good luck~
No, php couldn't do that without something on the client side. You could always have some javascript decode it, but that wouldnt be friendly to whoever has it turned off, it would be slow and no search engine support.