change url of current page to link to french version - php

I am building a client site with english and french versions.
On the english pages, I'd like users to be able to click a 'french version' link that would automatically take them to the french version OF THAT PAGE.
So if my url structure is like this:
http://mysite.com/en/page-name
I'd like the 'french version' link to point to:
http://mysite.com/fr/page-name
Can someone please offer me the php to take the current page url, and substitute the /en/ for /fr/ in the link code? I know this is probably something very simple, but I'm a php newb.

In your page template or rendering just use this for your link. Assume the current language is stored in a variable called $current_lang. At the top of your page add this code (it doesn't have to be at the top, just somewhere before you try to use the link):
<?php
$french_link = str_replace("/$current_lang/", '/fr/', $_SERVER['REQUEST_URI']);
?>
Now just use $french_link as your link to that page. So in your anchor link use this:
French Version
Note that most servers have PHP "short tags" enabled so you could use this for your link:
French Version
The <?= syntax is just shorthand for <?php echo. I personally prefer this syntax, but there are many on the other side who have good reasons for not using it. See this discussion for details.

Related

Best way to include different languages into a webpage

I would like to include a language menu on the top of my webpage where you can select between the languages English (en), Spanish (es) and German (de) (More languages might be added in the future). My website consists of the three pages nav.php, index.php and faq.php.
However, I am not sure about the best way to implement the languages on my website. So far I have come up with the following options:
(1) Create every page of the website in a different language and put it in the same folder:
website/pages/nav-en.php
/nav-es.php
/nav-de.php
/index-en.php
/index-es.php
...
(2) Create every page of the website in a different language and create an own folder for each language:
website/en/nav-en.php
/index-en.php
/faq-en.php
website/es/nav-es.php
/index-es.php
/faq-es.php
....
(3) With the options (1) and (2) described above I see the issue that code changes have to be done for each of the pages manually. Therefore, my solution would be to divide the functional code from the text content and loading the text file depending on the language that is selected in the menu:
website/pages/nav.php
/index.php
/faq.php
website/text-content/nav-text-en.php
/nav-text-es.php
/nav-text-de.php
/index-text-en.php
/index-text-es.php
....
Since I am a newbie in webdeveloping I wanted to ask you:
(a) Are the options described above the only once or is there another (better) way to include different languages on a webpage?
(b) Based on your experiences which option would you recommend?
(c) Is there anything else I need to consider?
Thanks for any advice.
You can use gettext for translation. This way you can have one file with structure and text in the same place for all languages, and translate your strings with an editor (like poedit), which could also be done by a translator. The translator would then only see the strings in one language and have to fill in the translated version, without having to deal with the HTML around it.
There is a shortcut to the gettext() function, _(), so you can write something like
<?=_("Linktext in different languages")?>
It's a bit more work to setup once, but after that, adding additional languages will be a lot easier than having different files with all the structure included, which will quickly become a maintenance nightmare.
(a) Are the options described above the only once or is there another (better) way to include different languages on a webpage?
I think using a $_GET param for both language and content is better way. For example,
index.php?page=faq&lang=en
(b) Based on your experiences which option would you recommend?
Using the power of php language is my advice. You may forget about static HTML website architecture.
(c) Is there anything else I need to consider?
If you're using apache, I suggest to use mod_rewrite. This way each page URL looks better:
index.php/en/faq
instead of
index.php?page=faq&lang=en
Well if you google at localization for PHP you will come up with multiple idea's. Personally I would use gettext. This way you can also format the numbers in the correct format.
My experience so far:
It's a nightmare to improve or even maintain a webpage where different views of the same content are based on different files, e.g. your start-page in english, german and spanish.
Therefore, it is a good approach to decouple the HTML-frame (+ CSS) from the language-dependent text. You can do that by implementing your own ideas ... or you are looking for a php framework which provides such functionality. I'm mainly a Java-guy so I don't know such framework. I guess Laravel and Zend Framework 2 could provide localization. Like others mentioned in the thread gettext also looks like a good way to realize localization in your project.
Hope that helps.
To build a multilangage website with easy maintenance, you can do this way :
1. You create ONE file by langage
en.php // english content
es.php // spanish content
...
2. Each file is an array with the same key, that content ALL the content you need to translate :
<?php
// en.php
$translate = array(
"menu_content" => array(
"home" => "Home page",
"nav" => "Nav page",
...
),
"nav_page_content" => array(
"title" => "Welcome to the nav page",
"content" => "In this page you will find...",
...
),
// same for ALL the content
);
3. Now, I see 2 ways you can add the content of your translation file in your website :
3.1 : You use $_SESSION['lang'] variable or some $_GET['lang'] variable if you have the current langage in your URL (eg. http://www.my-page?lang=en) and you load the php file you need at the begin of your PHP script. Then in your HTML you get the content according to your php array (the same key in ALL langage). For example using my example array for the menu :
<nav>
<ul>
<li id="home"> <?= $translate["menu_content"]["home"]; ?> </li>
<li id="nav"> <?= $translate["menu_content"]["nav"]; ?> </li>
...
</ul>
</nav>
And to change langage, you create a list of your langage and on click you change the $_SESSION['lang'] or $_GET['lang'] value and reload your page with the new php langage content.
3.2 : You can do almost the same without reload the page using $.ajax. The idea is almost the same : you have a list of all the langage of your website with a langage attribute for example :
<ul>
<li data-lg='es'>ES</li>
<li data-lg='en'>EN</li>
...
</ul>
On click, you you get this data-lg value and send it to your Ajax function, calling the php file of the choosen langage :
function changeLangue(lg) {
$.ajax({
type: "POST",
url: lg +".php", // eg. es.php, en.php, etc.
dataType: "json", // you will need to add "echo json_encode($translate);" at the end of your php file to send back JSON content
success: function(response){
// Here your change the content of your website without reloading the page, eg with my nav before:
$("#home").html(response.menu['home']);
$("#nav").html(response.menu['nav']);
...
},
error: function(x,e,t){
manageError(x,e);
}
});
With this method, you will need to change your current langage and rebuild your langage selection too but it's not that hard in JS/jQuery.
I don't know if it's the best way to do it to be honnest but it should work (I did it with the second method to test) and it's easy to add a new langage, you will only have to :
Create a new php file yournewlangage.php with the same array than before and translate the content
Add the yournewlangage to your langage list selection
And that's all if you did it right !
An other idea could be to use a Framework for you website. For example, I'm using Silex right now (micro-framework php), it's like Symfony but lighter and you can use the translation component to help you : https://silex.symfony.com/doc/2.0/providers/translation.html
Framework can help you to win some time if you can use them, take some time to use the best one for what you want to do !
Hope it's clear enought and it helps a little

language link to pick correct language file

I have a two language setup, structured like this:
includes/languages/lang-it [has the defines]
includes/languages/lang-en [has the defines]
includes/languages/languages [has the languages array]
en/files
en/subs/files
it/files
it/subs/files
I have en/sub-1/index.php and en/sub-1/file-1.php and need to be able to change language when in file-1.php page
This is the code I have, and of course it does'nt work
require("../../includes/languages/lang-en.php");
link to file-1 English
link to file-1 Italian
Where LANG_ENG and LANG_IT are the directories (en and it)
So I need it to pick the includes/languages/lang-en.php for the the English, and includes/languages/lang-it.php for the Italian.
I guess I need to "require" the correct language file before "LINK-1", but don't know how.
I solved it like this:
Added $thispage, added variables for the file-name, and put that in the link.
Perhaps there are better ways, but that's what I could think of, and is working.
<?php require("../../includes/languages/lang-en.php");?>
<?php // variables for pages to use for language links
$file_1_en="file-1-en.php";
$file_1_it="file-1-it.php";
?>
<?php if ($thisPage=="page name")
echo'
'?>

Make a link redirect to the same page

I've created a page with an article. On top of that there's a title. If people try to click this title I want them to be redirected to the same page.
Like this: https://gyazo.com/74350b4fe91c670c4101449ee1c928a4 If I click on the article it just refreshes the page.
I can't do this manually for every article because I'm using a script.
The code I've written looks like this:
echo ' <h1 class="entry-title">'.$row['postTitle'].'</h1>';
As you can see I wrote
<a href="/">
and that's will not redirect me / refresh the page i'm viewing.
This is how it looks for me: https://gyazo.com/8a15ae274d8a7240b07100395460568d
as you can see it does not redirect me to the same page when i click the title.
How can I do this?
To make your custom PHP blog post template be able to display a title link that points back to the page, one way is to make use of your $row[.... variable.
Provided that,
your URLS will look like your screenshots, such as http://localhost/viewpost.php?id=8 when running locally and for example http://www.yourwebsite.com/viewpost.php?id=8 when online
you know how to refer to the post's id that is used in the ...viewpost.php?id=8, for example $row['postID']
you don't yet have any variable or means to refer to your current domain http://localhost when local or http://www.yourwebsite.com when online
Then, I recommend a two-part approach:
Somewhere at the top of your code, or perhaps in an include you might use for such code-reuse purposes, define for example $host:
$host='http://' . $_SERVER['SERVER_NAME'];
Then, for your actual title link::
echo ' <h1 class="entry-title">' . $row['postTitle'] . '</h1>';
Explanation
Separated HTML from the concatenation dots . with spaces, to be easier to read, as well as to support any helper programs such as fmt that you might use for wrapping long lines, so they have spaces to use for wrapping lines.
Uses PHP's predefined $_SERVER variable's SERVER_NAME , which, combined with the http://, the $host will be http://localhost when local and http://www.yourwebsite.com when online.
Define $host as a variable once at the top of the page, because it is clearer that way and likely you will have a use for it elsewhere on the page, so this helps avoid having to repeat yourself writing 'http://'.$_SERVER['SERVER_NAME'] everywhere else you may need to start forming the absolute URL
$host is then combined with the pattern for the rest of the URL, to assemble the absolute URL
Absolute URL is helpful so that if a user saves your article to their computer, then later clicks the title link on their locally saved article, the user can still correctly reach the original online page
As the article author, setting a link this way also means it can serve as a permalink, which helps with search engine optimization (SEO)
Please use # in href attribute! that will redirect you on the same page.
Problem solved. I used
echo '<h1>'.$row['postTitle'].'</h1>';
Thank you everyone.

How to make clean links if I do no know the url

I try to design a web in different languages.
How can make the link that gives a clean url? Some pages are dynamic so, I do not know the url, I am searching for a way to make that links work in any page.
I know I can do this because it works in any page:
<a href='?lang=en'>en</a>
<a href='?lang=fr'>fr</a>
But, how to do that and be able to get a clean url like this?:
myDomain/someFolder/file.php // the default in English
myDomain/fr/someFolder/file.php // fr for French
(Beware that it is not just change fr to en. In English there is no /en/)
I have found that I have tu use a rewrite rule in the htaccess to change from myDomain/someFolder/file.php to myDomain/Folder/file.php?lang=en But this is not the point, my question is different, I ask: how to make clean links in the html if I do not know the url?
What I would do, which is not anywhere as fancy as an .htaccess rewrite tool...
First, I would do something to get the current page name. This can be as simple as:
$pagename = "contactme.html";
Now, I have an array of available languages:
$languages = array('en'=>'English','fr'=>'French','es'=>'Spanish');
With that, I can slap this on the top of every page:
Select your language: <?php
foreach($languages as $ln=>$language)
print " <a href='mysite.com/$ln/$pagename'>$ln</a> ";
That will spit out a list of abbreviations. Can they be little flags (a lot of people like those). Sure. Change $ln in the anchor to <img src='$ln.png' border='0'> and make sure you have all the images. Can they be in a form with a select list? Sure. Make a select list. Add an 'onchange' trigger to the form to reload to the correct URL. Can they be is a super-fancy rotating flash animation? Yes - if you really want to do something that silly.

Magento: how to include anchor link in URL rewrite

I'm very new to coding and hope someone can help me.
On Magento Commerce 1.7 I'm running 3 stores (3 languages). I have added an anchor link (#example) to point to a certain position on our policies (CMS) page, which is available in all three stores in their respective languages.
However, each policies (CMS) page has it's own URL in it's language. To switch from one store to the other I've had to use the URL rewrite in Magento so as not to get a 404.
English site: www.example.com/en/policies/#example
German site: www.example.com/de/agb_s/#example
Dutch site: www.example.com/nl/algemene_voorwaarden/#example
However, the URL rewrite doesn't accept the #anchor symbol and just adding it at the end of the URL in the php code means it just disappears as soon as the rewrite kicks in.
<?php echo $this->getUrl('policies/') ?>#example"
Is there a way to write the anchor into the php or the URL rewrite so that it doesn't get lost?
Thank you in advance.
OK, found it.
In the URL rewrite in Magento backend the "#" anchor symbol in the request path is not allowed but the "#" anchor symbol is allowed in the target path.
So, in the phtml file where you have the link, instead of this code:
<a href="<?php echo $this->getUrl('policies')?>#example" >example</a>
change the code to:
<a href="<?php echo $this->getUrl('policies/example')?>" >example</a>
and in Magento redirect this URL to:
English store: policies/example -> policies/#example
German store: policies/example -> agb_s/#example
Dutch store: policies/example -> algemene_voorwaarden/#example
The URL "policies/example" doesn't exist but that doesn't matter. The URL rewrite will redirect it to your anchor on the store's respective policies page.
Might not be pretty but it works!

Categories