Translating php webpage using gettext - php

I am translating this webpage however I the translation will only work when I add the parameter after .php (e.g. http://localhost/fr/about.php?lang=fr_FR)
How can i make it work like (e.g. http://localhost/fr/about.php/?lang=fr_FR)
<?php
/*
* Template Name: Test */
$url = $_SERVER["REQUEST_URI"];
$locale_lang = "en_EN";
if (substr($url,0,3) == "/fr/") { $locale_lang = "fr_FR"; }
if (substr($url,0,3) == "/en/") { $locale_lang = "en_US"; }
$lang = substr($locale_lang,0,2);
require_once("languages/lib/streams.php");
require_once("languages/lib/gettext.php");
$locale_file = new FileReader("languages/lib/$locale_lang/fr_FR.mo");
$locale_fetch = new gettext_reader($locale_file);
function _loc($text) {
global $locale_fetch;
return $locale_fetch->translate($text);
}
?>
<title><?php echo ("Title"); ?></title>
<h1><?php echo _loc("English Version"); ?></h1>

Related

Opening several javascript link from page

I am using "fabpot/goutte": "^3.2" and use PHP 7.3.5.
I am trying to access the following page and click the link - https://www.forexfactory.com/calendar.php?month=nov.2019 - to open the below box:
I tried to filter all this links, however no links are found:
$subCrawler->filter('td.calendar__cell.calendar__detail.detail > a')->each(function ($node) {
$link = $node->link();
print $link ."\n";
print $node->text() ."\n";
});
Any suggestions how to click the link with goutte and get the source text and usual effect text?
Using Goutte:
<?php
require 'vendor/autoload.php';
use Goutte\Client;
use Symfony\Component\DomCrawler\Crawler;
$x = 1;
$LIMIT = 20;
$client = new Client();
$crawler = $client->request('GET', 'https://www.forexfactory.com/calendar.php?month=nov.2019');
$resArray = array();
$TEMP = array();
$crawler->filter('.calendar_row')->each(function ($node) {
global $x;
global $LIMIT;
global $resArray;
global $TEMP;
$x++;
$EVENTID = $node->attr('data-eventid');
$API_RESPONSE = file_get_contents('https://www.forexfactory.com/flex.php?do=ajax&contentType=Content&flex=calendar_mainCal&details='.$EVENTID);
$API_RESPONSE = str_replace("<![CDATA[","",$API_RESPONSE);
$API_RESPONSE = str_replace("]]>","",$API_RESPONSE);
$html = <<<HTML
<!DOCTYPE html>
<html>
<body>
$API_RESPONSE
</body>
</html>
HTML;
$subcrawler = new Crawler($html);
$subcrawler->filter('.calendarspecs__spec')->each(function ($LEFT_TD) {
global $resArray;
global $TEMP;
$LEFT_TD_INNER_TEXT = trim($LEFT_TD->text());
if($LEFT_TD_INNER_TEXT == "Source"){
$TEMP = array();
$LEFT_TD->nextAll()->filter('a')->each(function ($LINK) {
global $TEMP;
array_push($TEMP,$LINK->text(),$LINK->attr('href'));
});
$EVENT['sourceTEXT'] = $TEMP[0];
$EVENT['sourceURL'] = $TEMP[1];
$EVENT['latestURL'] = $TEMP[3];
array_push($resArray,$EVENT);
}
});
if($x>$LIMIT){
echo "<pre>"; var_dump($resArray); echo "</pre>";
exit;
}
});
Using Simple HTML DOM. You can get it from here.
<?php
include('simple_html_dom.php');
$html = file_get_html('https://www.forexfactory.com/calendar.php?month=nov.2019');
$x = 1;
$LIMIT = 10;
foreach($html->find('.calendar_row') as $e){
$x++;
$EVENTID = $e->attr['data-eventid'];
$EVENTNAME = $e->find('.event')[0]->find('div')[0]->innertext;
echo "<h4>".$EVENTNAME."</h4><br>";
$API_RESPONSE = file_get_html('https://www.forexfactory.com/flex.php?do=ajax&contentType=Content&flex=calendar_mainCal&details='.$EVENTID);
$API_RESPONSE = str_replace("<![CDATA[","",$API_RESPONSE);
$API_RESPONSE = str_replace("]]>","",$API_RESPONSE);
$API_RESPONSE = str_get_html($API_RESPONSE);
foreach($API_RESPONSE->find('.calendarspecs__spec') as $LEFT_TD){
$LEFT_TD_INNER_TEXT = trim($LEFT_TD->innertext);
if($LEFT_TD_INNER_TEXT == "Source" || $LEFT_TD_INNER_TEXT == "Usual Effect"){
echo $LEFT_TD_INNER_TEXT.": ".$LEFT_TD->next_sibling()->innertext."<br>";
}
}
if($x>$LIMIT)
break;
echo "<hr>";
}
Screenshot(Goutte):
Screenshot(SIMPLE HTML DOM):

?url= does not work

I am coding for a site which use admin panel and member panel both panels files are located in two separate folders named master and member. Both have main.php file which are header files from those files i redirect to other files, Both main.php use same function file to redirect url by ?url= .
Now problem is admin main.php redirect to correct url but member main.php returns back without showing errors.
function.php is
<?php
function logout($destinationPath)
{
if(count($_SESSION))
{
foreach($_SESSION AS $key=>$value)
{
session_unset($_SESSION[$key]);
}
session_destroy();
}
echo "<script language='javaScript' type='text/javascript'>
window.location.href='".$destinationPath."';
</script>";
}
function validation_check($checkingVariable, $destinationPath)
{
if($checkingVariable == '')
{
echo "<script language='javaScript' type='text/javascript'>
window.location.href='".$destinationPath."';
</script>";
}
}
function realStrip($input)
{
return mysql_real_escape_string(stripslashes(trim($input)));
}
function no_of_record($table, $cond)
{
$sql = "SELECT COUNT(*) AS CNT FROM ".$table." WHERE ".$cond;
$qry = mysql_query($sql);
$rec = mysql_fetch_assoc($qry);
$count = $rec['CNT'];
return $count;
}
//drop down
function drop_down($required=null, $text_field, $table_name, $id, $name, $cond, $selected_id=null)
{
$qry = mysql_query("SELECT $id, $name FROM $table_name WHERE $cond ORDER BY $name ASC");
$var = '';
if(mysql_num_rows($qry)>0)
{
$var = '<select id="'.$text_field.'" name="'.$text_field.'" '.$required.'>';
$var .='<option value="">--Choose--</option>';
while($r = mysql_fetch_assoc($qry))
{
$selected = '';
if($selected_id==$r[$id]){
$selected = 'selected="selected"';
}
$var .='<option value="'.$r[$id].'" '.$selected.'>'.$r[$name].'</option>';
}
$var .='</select>';
}
echo $var;
}
function uploadResume($title,$uploaddoc,$txtpropimg)
{
$upload= $uploaddoc;
$filename=$_FILES[$txtpropimg]['name'];
$fileextension=strchr($filename,".");
$photoid=rand();
$newfilename=$title.$photoid.$fileextension;
move_uploaded_file($_FILES[$txtpropimg]['tmp_name'],$upload.$newfilename);
return $newfilename;
}
function fRecord($field, $table, $cond)
{
$fr = mysql_fetch_assoc(mysql_query("SELECT $field FROM $table WHERE $cond"));
return $fr[$field];
}
function get_values_for_keys($mapping, $keys) {
$output_arr = '';
$karr = explode(',',$keys);
foreach($karr as $key) {
$output_arr .= $mapping[$key].', ';
}
$output_arr = rtrim($output_arr, ', ');
return $output_arr;
}
function getBaseURL() {
$isHttps = ((array_key_exists('HTTPS', $_SERVER)
&& $_SERVER['HTTPS']) ||
(array_key_exists('HTTP_X_FORWARDED_PROTO', $_SERVER)
&& $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
);
return 'http' . ($isHttps ? 's' : '') .'://' . $_SERVER['SERVER_NAME'];
}
function request_uri()
{
if ($_SERVER['REQUEST_URI'])
return $_SERVER['REQUEST_URI'];
// IIS with ISAPI_REWRITE
if ($_SERVER['HTTP_X_REWRITE_URL'])
return $_SERVER['HTTP_X_REWRITE_URL'];
$p = $_SERVER['SCRIPT_NAME'];
if ($_SERVER['QUERY_STRING'])
$p .= '?'.$_SERVER['QUERY_STRING'];
return $p;
}
preg_match ('`/'.FOLDER_NAME.'(.*)(.*)$`', request_uri(), $matches);
$tableType = (!empty ($matches[1]) ? ($matches[1]) : '');
$url_array=explode('/',$tableType);
?>
In main.php
<li><a href='?url=epin/used.php'><span>Used e-Pin</span></a></li>
is used to redirect url
http://localhost/abcd.biz/secure/master/main.php?url=epin/used.php is working
and http://localhost/abcd.biz/secure/member/main.php?url=epin/used.php is not working
Both main.php files are using same function file so I can't get the reason please help to get out of it, thanks in advance, try to clarify it also.
I am not using any htaccess file

How to store data in a single file and call it in view file?

I have some code that i keep rewriting on different pages on my site so i keep re writing the code over and over on each view file.
I would like to know how i can change the below code so that i can have the below code in only one file. Is it possible to call this file in view file ?
if ($model->type == 1) {
$title = __('Sports');
$icon_activity = 'icon_set_2_icon-117';
}
if ($model->type == 2) {
$title = __('Cooking lesson');
$icon_activity = 'icon_set_1_icon-14';
}
if ($model->type == 3) {
$title = __('Guided tour');
$icon_activity = 'icon_set_1_icon-30';
}
if ($model->type == 4) {
$title = __('Language conversation');
$icon_activity = 'icon_set_1_icon-20';
}
Writing logic in the view file is not a good practice. Instead, you can make a class and call it in the controller's action. For example, I make this class:
Tools.php:
<?php
class Tools{
public function initialize($type){
$title = "";
$icon_activity = "";
if ($type == 1) {
$title = __('Sports');
$icon_activity = 'icon_set_2_icon-117';
}
if ($type == 2) {
$title = __('Cooking lesson');
$icon_activity = 'icon_set_1_icon-14';
}
if ($type == 3) {
$title = __('Guided tour');
$icon_activity = 'icon_set_1_icon-30';
}
if ($type == 4) {
$title = __('Language conversation');
$icon_activity = 'icon_set_1_icon-20';
}
return array("title"=>$title, "icon_activity"=>$icon_activity);
}
}
Now, in your action you can do this:
public function actionTest(){
...
$tools = new Tools();
$result = $tools->initialize($model->type)
$this->render("yourViewName", array(
"title" => $result['title'],
"icon_activity" => $result['icon_activity']
));
}
Finally, you can use $title and $icon_activity in your view page.

Why php function doesn't work inside second <?php ?>

Why this works perfect:
<?php
$url = $_SERVER["REQUEST_URI"];
$locale_lang = "pl_PL";
if (substr($url,0,3) == "/pl") { $locale_lang = "pl_PL"; }
if (substr($url,0,3) == "/en") { $locale_lang = "en_US"; }
$lang = substr($locale_lang,0,2);
require_once("lib/streams.php");
require_once("lib/gettext.php");
$locale_file = new FileReader("locale/$locale_lang/LC_MESSAGES/messages.mo");
$locale_fetch = new gettext_reader($locale_file);
function _loc($text) {
global $locale_fetch;
return $locale_fetch->translate($text);
}
echo "<!doctype html>
<html lang=\"$lang\">
<head>
<title>"._loc("Summoners War")."</title>";
?>
when this doesn't work. Function _loc return empty value. There are no php notice/error
<?php
$url = $_SERVER["REQUEST_URI"];
$locale_lang = "pl_PL";
if (substr($url,0,3) == "/pl") { $locale_lang = "pl_PL"; }
if (substr($url,0,3) == "/en") { $locale_lang = "en_US"; }
$lang = substr($locale_lang,0,2);
require_once("lib/streams.php");
require_once("lib/gettext.php");
$locale_file = new FileReader("locale/$locale_lang/LC_MESSAGES/messages.mo");
$locale_fetch = new gettext_reader($locale_file);
function _loc($text) {
global $locale_fetch;
return $locale_fetch->translate($text);
}
?>
<!doctype html>
<html lang="<?php echo $lang; ?>">
<head>
<title><?php _loc("Summoners War"); ?></title>
I can leave it in first (working) form, but then html code is inside php and is difficult to read
In first case you put your _loc function inside echo which is absent in your second example.
change your last line code to:
<title><?php echo _loc("Summoners War"); ?></title>
Put echo before _loc function for printing its output

Change title on page

I have the following code that change Title on the page:
class.php
<?php
class Title_And_Page
{
public $pagekey;
public $title;
public $page;
private $pages = array(
'start_page' => array("Startsida", "start_page.php"),
'products' => array("Produkter", "products.php"),
'max_ot' => array("Max-OT", "max_ot.php"),
'blog' => array("Blogg", "blog.php"),
'tools' => array("Verktyg", "tools.php"),
'about_us' => array("Om oss", "about_us.php"));
public function __construct($pagekey)
{
$this->pagekey = $pagekey;
}
public function setTitle()
{
if(array_key_exists($this->pagekey, $this->pages))
{
$this->title = $this->pages[$this->pagekey][0]; //Returns the value in title, that it gets when the constructs is run
return $this->title;
}
}
public function includePage()
{
if(array_key_exists($this->pagekey, $this->pages))
{
$this->page = $this->pages[$this->pagekey][1]; //Returns the value in page, that will be included
return $this->page;
}
}
}
?>
Here is my some code from my index.php
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
$page_title = new Title_And_Page($page);
<title><?= $page_title->setTitle(); ?></title>
<li id="info"><a href="?page=products" class='clickme'>Produkter</a></li>
<li id="info">MAX-OT</li>
<li id="info">Blogg</li>
<li id="info">Verktyg</li>
<li id="info">Om oss</li>
This works. However, I have articles in the blog page that contains "Read more"-links. When I click on a "Read more"-link, the URL changes to this: index.php?page=blog&readmore_from_firstpage=1&article_header=Vilken kolhydrat är bäst att äta efter träningen?
How can I change the title of the page, to the value in $_GET['article_header'] as you can see above?
Just extend your GET checks:
if(isset($_GET['article_header']))
{
$page = $_GET['article_header'];
}
elseif(isset($_GET['page']))
{
$page = $_GET['page'];
}
But since you're checking in your class whether that page is whitelisted, you'd need to either add another variable to force avoiding such check or simply just to print out the title if article_header is present.
Here's an example of the latter:
$avoidClass = false;
if(isset($_GET['article_header']))
{
$page = $_GET['article_header'];
$avoidClass = true;
}
elseif(isset($_GET['page']))
{
$page = $_GET['page'];
}
Then in HTML:
<title><?= $avoidClass ? $page : $page_title->setTitle(); ?></title>
Or, probably the simplest way:
if(isset($_GET['article_header']))
{
$page_title = $_GET['article_header'];
}
elseif(isset($_GET['page']))
{
$page_title = new Title_And_Page($_GET['page'])->setTitle();
}
else
{
$page_title = 'Default title here';
}
HTML
<title><?= $page_title ?></title>

Categories