A footer in different languages - php

I have a web in different languages. I make an include for the footer. The easiest way could be to have a different footer for each language. But is it possible to have just one footer and change the few sentences that are different in each language?
In all pages put the same include:
<?php include('footer.php'); ?>
Then, in the includes just change what is different. Something like:
<footer>
<?php echo $text; ?> <br><br>
</footer>
</body>
</html>
<?php
if ('<html lang="en">')
$text = 'Some text in English';
elseif ('<html lang="fr">')
$text = 'Français';
?>
(In each page I have the html lang= )
What is the better way to have a footer in different languages?
(I am just learning php, so please, just help me with the basics, where to begin)

Okay so first you need to create translation files for all languages you wish to support. Store them in "/lang/en.php" and "/lang/fr.php".
"lang/en.php"
<?php
return [
"title" => "My site",
"welcome" => "Welcome",
"goodbye" => "Goodbye"
]
?>
"lang/fr.php"
<?php
return [
"title" => "Mon site",
"welcome" => "Bienvenue",
"goodbye" => "Au revoir"
]
?>
Next, you include the appropriate language file in your php page:
"index.php"
<?php
$locale = $_SESSION['locale']; // this is "en" or "fr", depending on a choice the user made earlier
$lang = require("/lang/$locale.php"); // load "/lang/en.php" or "/lang/fr.php"
$user = $_SESSION['username']; // e.g. "Bart"
?>
<html>
<head>
<title><?php echo $lang['title']; ?></title>
</head>
<body>
<?php include('header.php'); ?>
<main>content</main>
<?php include('footer.php'); ?>
</body>
</html>
And in your header/footer you can just use $lang as well:
"header.php"
<header>
<p><?php echo $lang['welcome'] . ', ' . $user; ?></p>
</header>
It's important to know that you should include your language file only in pages that a user will view directly (i.e. don't include it in partial views like header.php)

You can create a poor man's translation function:
function translate($sentence, array $vars = null, $lang = 'en') {
static $table = array();
if ( ! isset($table[$lang])) {
$table[$lang] = require(ROOT."/lang/{$lang}.php");
}
$trans = isset($table[$lang][$sentence]) ? $table[$lang][$sentence] : $sentence;
if ( ! empty($vars)) {
$trans = strtr($trans, $vars);
}
return $trans;
}
You can then create some language files, such as:
<?php
// ROOT/lang/de.php
return [
'Welcome :name' => 'Willkommen :name',
'Thank you' => 'Danke',
];
And then in your scripts you can translate stuff:
<header>
<?php echo translate('Welcome :name', [':name' => 'Bob'], 'de') ?>
</header>
Instead of using a function you could also just include the language file and then use that.
<?php
// some-page.php
$lang = require(ROOT."/lang/{$_SESSION['user.lang']}.php");
$name = $_SESSION['user.name'];
?>
<header>
<?php echo str_replace(':name', $name, $lang['Welcome :name']) ?>
</header>
It will require you to do some more work, but if you find it to be more to your liking then okay.

You could set a session that contains the language such as:
<?php
session_start();
$_SESSION['language'] = "EN";
?>
Then in the footer:
<?php
switch($_SESSION['language']) {
case 'EN':
$sentences['site_slogan'] = "This is your site slogan";
$sentences['site_messag'] = "This is your site message";
break;
case 'FA':
$sentences['site_slogan'] = "This is your site slogan in FA";
$sentences['site_messag'] = "This is your site message in FA";
break;
}
echo $sentences['site_slogan'];
echo $sentences['site_messag'];

Related

How to set dynamic titles in one page template embedding different pages inside the body tag - PHP

According to single page template structure there's only one file called index.php that embeds pages in it using PHP function include_once() inside the body tag against the query string $_GET['page']
<?php $page = $_GET['page']; ?>
<html>
<head>
<title>
<!-- title needs to update according to page here -->
</title>
</head>
<body>
<?php require_once MC_ROOT.'/pages/'.$page.'.php'; // page content goes here like: <div> [CONTENT], <h1> [PRODUCT TITLE] </h1> </div> etc... ?>
</body>
</html>
How do i set dynamic titles inside <title></title> tags which comes before for each page? Pages are supposed to be home, product, contact, about etc. Please suggest any best practice. Thank you
you can use pars_url(This function parses a URL)
for exam
if you address like this
$url = "http://www.ibm.com/product?id=1";
echo parse_url($url, PHP_URL_PATH);
////echo product /////
OR
$url = 'http://www.ibm.com/product/il.php?id=value';
$var = parse_url($url, PHP_URL_PATH);
$var = explode('/', $url);
echo $var[3];
///echo product////
OR
$url = 'http://www.ibm.com/aboutUs.php';
$var = parse_url($url, PHP_URL_PATH);
$var = explode('/', $url);
$var = str_replace(".php","",$var[3]);
echo $var;
///echo aboutUs////
You can include a title-page mapping:
mapping.php
$mapping = [
'home' => 'Welcome on my homepage',
'product' => ['242' => 'My fidget Spinner']
];
$title = '';
if(isset($_GET['page'])){
if(isset($mapping[$_GET['page']])){
$title = $mapping[$_GET['page']];
}
}
if(isset($_GET['page']) && isset($_GET['id'])){
if(isset($mapping[$_GET['page']][$_GET['id']])){
$title = $mapping[$_GET['page']][$_GET['id']];
}
}
you should use ob_start :
ob_start();
$header=MC_ROOT.'/pages/'.$page.'.php';
include($header);
$buffer=ob_get_contents();
ob_end_clean();
$buffer=str_replace("%TITLE%","NEW TITLE",$buffer);
echo $buffer;

Dynamic content / titles / meta tags

I just created a new website using a tutorial as reference. My problem is mainly the title that doesn't change. But it's not possible to change anything either. Everything is static in the header defined.
I'm brand new to PHP, so any help appreciated.
This is my current code:
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
if (isset($_GET['content'])) {
$content = strtolower(trim($_GET['content']));
} else {
$content = '';
}
include_once 'inc/header.php';
switch ($content) {
case 'main':
include 'inc/main.php';
break;
case 'blog':
include 'inc/blog.php';
break;
case 'portfolio':
include 'inc/portfolio.php';
break;
case 'lebenslauf':
include 'inc/lebenslauf.php';
break;
case 'kontakt':
include 'inc/kontakt.php';
break;
default:
include 'inc/main.php';
}
$content = "main";
include_once 'inc/footer.php';
?>
Do you know how to edit this code to be able to change titles or meta tags dynamically or do you know a tutorial for something like a best practice include of PHP files?
Absolutely simplest bare-bones "dynamic" header:
header.php:
<title><?php echo $title ?></title>
index.php:
$title = 'Hello, world!';
include('header.php');
Try below Code.
<?php
$title = "";
$description = "";
$keywords = "";
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(strpos($url, 'index') !== false){
$title = "Home | Your Site Name";
$description = "Your Site description ";
$keywords = "Your Site Keywords";
}elseif(strpos($url, 'about') !== false){
$title = "About Us";
$description = "About description ";
$keywords = "key words";
}
?>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
I'm not sure of the reason why you have routed to pages like this, however i'm guessing your titles/metas are in the header.php file.
To change them you can create an array for each like this:
$titles = array(
'blog.php' => 'title for blog',
'main.php' => 'title for main',
//etc
);
In your switch, assign a variable $include or something the correct page eg:
case:'main':
$include = 'main.php';
include(main.php);
break;
Then use that $include to get the title in your header.php, e.g:
<title><?php echo $titles[$include];?></title>

Setting Page Titles When Using Global Headers in PHP

I was wondering how you guys set your page titles while you use global headers. I would like to be able to change my page title from page to page... for example, "Site Name : News Archives". Would the best way be to use JavaScript? If I did this with JS, would the new changes take effect in search engine results? Just wanted to get some input on this thought.
<?php
include('header.php');
switch($_GET['p']){
case "news":
include('news.php');
break;
default:
include('indexBody.php');
}
include('footer.php');
?>
You can define a variable BEFORE including the header.
This variable can be used then in header.php.
<?php
$pagetitle = "Site Name : News Archives";
include('header.php');
...
?>
If your looking to go down this road, perhaps try something like the following:
Store your variables in a data array and then pass them to your header/footer/content views before echo'ing out.
$data['title'] will be seen by your header and footer ect as $title
<?php
$page = (!empty($_GET['p'])?$_GET['p']:'index');
$data = array();
switch($page){
case "news":
$view = 'news.php';
$data['title'] = 'Site Name : News Archives';
break;
default:
$view = 'indexBody.php';
$data['title'] = 'Site Name : Home';
break;
}
echo load_view('header.php',$data);
echo load_view($view,$data);
echo load_view('footer.php',$data);
function load_view($path,$data) {
if (file_exists($path) === false){
return 'View not found: '.$path;
}
extract($data);
ob_start();
require($path);
$out = ob_get_contents();
ob_end_clean();
return $out;
}
?>

Dynamic Title Tag in PHP

I am trying to dynamically populate the title tag on a website. I have the following code in my index.php page
<?php $title = 'myTitle'; include("header.php"); ?>
And the following on my header page
<title><?php if (isset($title)) {echo $title;}
else {echo "My Website";} ?></title>
But no matter what I do, I cannot get this code to work. Does anyone have any suggestions?
thanks
This works (tested it - create a new folder, put your first line of code in a file called index.php and the second one in header.php, run it, check the title bar).
You should double check if those two files are in the same folder, and that you're including the right header.php from the right index.php. And ensure that $title is not being set back to null somewhere in your code.
Learn more about Variable Scope here.
Edit: Examples of visible changes would be:
TEST1<?php $title = 'myTitle'; include("header.php"); ?>
<title>TEST2<?php if ...
Are you including the header file before or after you set the title variable? If you're including it before, then of course it won't be set.
if you're doing something like this in your index.php:
<?php
include('header.php');
$title = "blah blah blah";
?>
then it won't work - you include the header file and output the title text before the $title variable is ever set.
try to declare the variable before using it
$title = '123';
require 'includes/header.php';
Hi Try this old school method ..
In your Header file (for e.g. header.php)
<?php
error_reporting(E_ALL);
echo '<!DOCTYPE html>
<!--[if IE 7 ]><html class="ie7" lang="en"><![endif]-->
<!--[if IE 8 ]><html class="ie8" lang="en"><![endif]-->
<!--[if IE 9 ]><html class="ie9" lang="en"><![endif]-->
<!--[if (gte IE 10)|!(IE)]><!-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<!--<![endif]-->
<head>';
?>
<?php
if($GLOBALS['title']) {
$title = $GLOBALS['title'];
} else {
$GLOBALS['title'] = "Welcome to My Website";
}
if($GLOBALS['desc']) {
$desc = $GLOBALS['desc'];
} else {
$desc = "This is a default description of my website";
}
if($GLOBALS['keywords']) {
$keywords = $GLOBALS['keywords'];
} else {
$keywords = "my, site, key, words";
}
echo "\r\n";
echo "<title> ". $title ." | MyWebsite.com </title>";
echo "\r\n";
echo "<meta name=\"description\" content='". $GLOBALS['title']."'>";
echo "\r\n";
echo "<meta name=\"keywords\" content='".$GLOBALS['title']."'>";
echo "\r\n";
?>
In you PHP Page file do like this (for example about.php)
<?php
$GLOBALS['title'] = 'About MyWebsite -This is a Full SEO Title';
$GLOBALS['desc'] = 'This is a description';
$GLOBALS['keywords'] ='keyword, keywords, keys';
include("header.php");
?>
I assume your header is stored in a different file (could be outside the root directory) then all the above solutions will not work for you because $title is set before it is defined.
Here is my solution:
in your header.php file you need to set the $title to be global by: global $title; then echo it in your title so:
<?php global $title; ?>
<title><?php echo isset($title) ? $title : "{YOUR SITE NAME}"; ?></title>
Then in every page now you can define your title after you have included your header file so for example in your index.php file:
include_once("header.php");
$title = "Your title for better SEO"
This is tested and it is working.
We can also use functions and its a good way to work on real time web sites.
Do simple:
create an index.php file and paste these lines:
<?php include("title.php");?>
<!doctype html>
<html>
<head>
<title><?php index_Title(); ?></title>
<head>
</html>
-- Then
Create a title.php file and paste these lines:
<?php
function index_Title(){
$title = '.:: itsmeShubham ::.';
if (isset($title)){
echo $title;
}else{
echo "My Website";
};
}
?>
It will work perfectly as you want and we can also update any title by touching only one title.php file.
<?php
echo basename(pathinfo($_SERVER['PHP_SELF'])['basename'],".php");
?>
This works. Since I'm using PHP I don't check for other extensions; use pathinfo['extension'] in case that's required.
You can achieve that by using define(); function.
In your header.php file add following line :
<title><?php echo TITLE; ?></title>
And on that page where you want to set dynamic title, Add following lines:
EX : my page name is user-profile.php where I want to set dynamic title
so I will add those lines that page.
<?php
define('TITLE','User Profile'); //variable which is used in header.php
include('header.php');
include('dbConnection.php');
?>
So my user-profile/.php file will be having title: User Profile
As like this you can add title on any page on your site
Example Template.php
<?php
if (!isset($rel)) {$rel = './';}
if (!isset($header)) {
$header = true;
?><html>
<head>
<title><?php echo $pageTitle; ?></title>
</head>
<body>
<?php } else { ?>
</body>
</html><?php } ?>
Pages Your Content
<?php
$rel = './'; // location of page relative to template.php
$pageTitle = 'This is my page title!';
include $rel . 'template.php';
?>
Page content here
<?php include $rel . 'template.php'; ?>
I'm using your code in my project and it works properly
My code in header:
<title>
<?php
if (isset($title)) {echo $title;}
else {echo "عنوانی پیدا نشد!";}
?>
</title>
and my code in index.php:
<?php
$title = "سرنا صفحه اصلی";
include("./include/header-menu.php");
?>

How to make something to let the user change the language in a multilingual website that uses php arrays?

I followed the following tutorial to build a multilingual webpage using php arrays: http://bytes.com/topic/php/answers/880915-how-develop-multi-lingual-websites-php
the code:
files tree:
locale/
english.php
french.php
set_locale.php
index.php
locale/english.php:
<?php
$locale = array(
'title' => 'Title in English',
'h1' => 'The following in in English:',
'p1' => 'This is a sample text, in English'
);
?>
locale/french.php:
<?php
$locale = array(
'title' => 'Titre en français',
'h1' => 'Le texte suivant en en français:',
'p1' => 'Il s\'agit d\'un échantillon de texte, en français.'
);
?>
set_locale.php:
<?php
// Get the language from the query string, or set a default.
($language = #$_GET['lang']) or $language = 'english';
// Set up a list of possible values, and make sure the
// selected language is valid.
$allowed_locales = array('english', 'french');
if(!in_array($language, $allowed_locales)) {
$language = 'english'; // Set default if it is invalid.
}
// Inlclude the selected language
include "locale/{$language}.php";
// Make it global, so it is accessible everywhere in the code.
$GLOBALS['L'] = $locale;
?>
index.php:
<?php
// Include the "set_locale" script to fetch the locale
// that should be used.
include_once "set_locale.php"
// Print the HTML, using the selected locale.
?>
<html>
<head>
<title><?php echo $GLOBALS['L']['title']; ?></title>
</head>
<body>
<h1><?php echo $GLOBALS['L']['h1']; ?></h1>
<p><?php echo $GLOBALS['L']['p1']; ?></p>
</body>
</html>
Now, I want to make something to let the user click on a link and change the language
Something like this:
<ul id="language-selection">
<li>English</li>
<li>English</li>
</ul>
What's the best way of doing it?
Pass language as part of the query string:
<ul id="language-selection">
<li>English</li>
<li>English</li>
</ul>
In your code, you are using $_GET['lang'], so this should catch it. You may want to mofify the code if the need be.
index.php?lang=english
OR
index.php?lang=french

Categories