i am trying to localize my code and i want to use gettext and poedit to do so it seems like the most straight forward way to do it.
I initialize all my classes and views from a simple script which includes this.
I am struggling to understand how the gettext() function works here is an example of what i mean:
File structure of the translation from root: i18n/Locale/da_DK/LC_MESSAGES.
The messages.po and .mo is in the LC_MESSAGES folder and if my scripts are in the i18n folder and i call c.php in the browser it works
If i put the c.php in a folder further apart it doesn't work so the path is: someFolder/i18n/Locale/da_DK/LC_MESSAGES
and the c.php is in someFolder and includes a.php and b.php from i18n.
scripts:
a.php
<?php
// use sessions
session_start();
// get language preference
if (isset($_GET["lang"])) {
$language = $_GET["lang"];
}
else if (isset($_SESSION["lang"])) {
$language = $_SESSION["lang"];
}
else {
$language = "da_DK";
}
// save language preference for future page requests
$_SESSION["Language"] = $language;
$folder = "Locale";
$domain = "messages";
$encoding = "UTF-8";
putenv("LANG=" . $language);
setlocale(LC_ALL, $language);
bindtextdomain($domain, $folder);
bind_textdomain_codeset($domain, $encoding);
textdomain($domain);
b.php
<?php
echo _('Change language');
?>
c.php
<?php
include('a.php');
include('b.php');
?>
c.php (in someFolder)
<?php
include('i18n/a.php');
include('i18n/b.php');
?>
Most probably it's all because of your Folder definition:
$folder = "Locale";
When you move your file to someupper folder, you should modify path to something like:
$folder = "someFolder/Locale";
Let me know if this helps.
EDIT:
Or even better, hardcode your path if you know it:
$folder = "/home/me/myproject/someFolder/i18n/Locale";
Related
I would like to use a <?php include ... command to choose one random php file from one folder.
So this is the basic idea:
<?php include 'random file: 1.php, 2.php, 3.php or 4.php';?>
I have already read articles like this or this, but they don't answer my question clearly. What is the easiest way to do that?
// Directory to use
$directory = '.';
// Filter out directories, we only want files.
$files = array_filter(scandir($directory), fn($f) => is_file($f));
// Pick a random file
$randFile = $directory . '/' . $files[array_rand($files)];
// Include it
include $randFile;
to include random files from another directory
$path = __DIR__."/folder";
foreach(new DirectoryIterator($path) as $file){
if($file->isFile()){
$arr[] = $file->getFilename();
}
}
$randFile = $path."/".$arr[array_rand($arr)];
include $randFile;
I understand in HTML, you can use ../../../ to include content which is included in other folders.
I have this PHP code and I'm wondering how can I use ../../../ before header.php?
<?php
ob_start();
include("../../../header.php");
$buffer=ob_get_contents();
ob_end_clean();
$buffer=str_replace("%TITLE%","Homepage",$buffer);
echo $buffer;
?>
you can use chdir() php function and getcwd(). im suggest you use constant as source path location..
try this:
<?php
ob_start();
chdir("../../../");
$cwd = rtrim(str_replace("\\", "/", getcwd()), '/').'/';
include($cwd."header.php");
$buffer=ob_get_contents();
ob_end_clean();
$buffer=str_replace("%TITLE%","Homepage",$buffer);
echo $buffer;
?>
I have a file called index.php and in this file there is a variable called $mainDir, I would like this variable to be used in the file that is included,
Example:
<?php // index.php
$mainDir = dirname(__FILE__);
if ($isTest){ // presume this is true
include $mainDir . 'test.php';
}
?>
// Different File:
<?php // test.php
echo $mainDir;
?>
I have looked at the other solutions and I have placed the variable I want before the include statement and the include statement is being called properly, it just gives me a warning that the variable that I want to use in the included file is not set.
dirname(__FILE__); doesn't include a "/" at the end I believe. To fix this, simply use this as your include:
include $mainDir.'/test.php';
Maybe you can try this :
<?php // index.php
$mainDir = dirname(__FILE__);
if ($isTest){ // presume this is true
include $mainDir . 'test.php';
}
$_GET["mainDir"] = $mainDir;
?>
// Different File:
<?php // test.php
echo $_GET["mainDir"];
?>
it's horrible but it should works.
I am trying to build my own simple template handler to easily be able to nest and combine files into variables and call these variables when neccessary, as shown below:
$partial['header'] = 'header.php';
foreach($partial as $part => $view ) {
$output[$name] = file_get_contents( APPPATH .'views/' . $view . '.php' );
}
extract($output, EXTR_PREFIX_ALL, 'template' );
include 'mytemplate.php';
The mytemplate.php template file:
<?php
echo $template_header; // Shows the header.
The question:
Obviously, loading a PHP file by file_get_contents isn't going to call any PHP code inside the loaded file and I am sure that there's better options available then using eval. What should I change to be able to use PHP inside my template files?
More ugly is possible but doing exactly what you're wanting :
function custom_get_content($filename){
if (is_file($filename)) {
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
}
Then you can call it :
foreach($partial as $part => $view ) {
$output[$name] = custom_get_content( APPPATH .'views/' . $view . '.php' );
}
I copied that from a comment in the PHP manual but can't find it anymore (and that's why maybe it's too ugly :D)
Reading the scripts in is obiously not the best approach. You'll have to eval them.
And just for the record:
include($filename);
Is functionally identical to:
eval("?>" . file_get_contents($filename));
Get over it. The "eval is evil" meme is just that, a meme. So if you want to keep your appraoch, you could just use eval("?>$template_header"); instead of echo.
The alternative would be to skip the file reading, and have your $template_vars contain filenames rather than their content. Then do an ordinary include($template_header);
Hi
I have a php file at, say, localhost/foo/foo/bar.php
which includes a file at localhost/foo/included.php
I need to be able to get "localhost/foo/" as a string inside included.php
If, instead of localhost/foo/foo/bar.php, it's localhost/big/burpy/lolz/here.php (still including included.php) I still need to get "localhost/foo/"
So, I need the path of the included file and not the one that the client requested.
I know when I see the solution I'm going to feel like a doofus, but it just escapes me at the moment. Help please? thanks :)
This how it got working for me :)
<?php
$path = (#$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
$path .=$_SERVER["SERVER_NAME"]. dirname($_SERVER["PHP_SELF"]);
echo $path;
?>
I figured it out myself:
$realpath = str_replace('\\', '/', dirname(__FILE__));
$whatIwanted = substr_replace(str_replace($_SERVER['DOCUMENT_ROOT'], '', $realpath), "", -6);
There we go :) Thanks for the help guys.
Inside your included file:
$yourdir = dirname(__FILE__);
or if you're using PHP 5.3.x:
$yourdir = __DIR__;
Get the document root from
// contains the document root, e.g. C:\xampp\htdocs
$docRoot = realpath($_SERVER['DOCUMENT_ROOT']);
// strip drive letter if found
if(strpos($docRoot, ':') === 1) $docRoot = substr($docRoot, 2);
// directory of included file, e.g. C:\xampp\htdocs\include
$dirInclude = realpath(dirname(__FILE__));
// strip drive letter if found
if(strpos($dirInclude, ':') === 1) $dirInclude = substr($dirInclude, 2);
// find the document root
$rootPos = strpos($dirInclude, $docRoot);
// if the path really starts with the document root
if($rootPos === 0){
// example: \xampp\htdocs\include
$visibleDir = substr($rootPos, $);
// convert backslashes to slashes and strip drive letter
$webPath = str_replace('\\', '/', $visibleDir);
// yields: http://localhost/include
echo 'http://localhost' . $webPath;
}
else{
// included file was outside the webroot, nothing to do...
}
The steps for this are:
Use dirname(__FILE__) to get the folder of the include file.
Get the server root using $_SERVER['DOCUMENT_ROOT']
Remove the document root from the include folder to get the relative include folder
Obtain the server url
Append the relative include folder to the server url