Joomla conditional language display - php

I try to display a field depending on joomla language
I tried many versions like this but didnt succeded.
<?php
$lang = JFactory::getLanguage();
if($lang == 'ro-ro') {
echo 'Current language is: ' . $lang->getTag();
}
?>
I need a code for 3 languages.When is romanian to display an message when is english another message , and on french 3rd different message.
Can anyone help me please?
Thank you

Related

staying on the current page when changing language

I'm setting up a language switcher for my website but have an issue with the actual switcher which always brings back to the homepage (index.php). :-s very annoying
I a complete newbie in php and have been following this tutorial to do so on youtube: https://www.youtube.com/watch?v=cgvDMUrQ3vA
I am trying to implement it on a multi pages website. The actual language swap works like a charm and seems to keep the set language throughout the pages but the mechanism for the switch doesn't because it uses a static link to load index.php + new language attribute, bringing the visiter inevitably back to the homepage instead of reloading the current page.
<?php echo $lang['lang-selector-href'] /* set the <a> tag src="index+lang='x'" */ ?>
<?php echo $lang['lang-selector-switch'] /* display language to be set */ ?>
</a>
i'm using 2 similar files (en.php and fr.php) containing an array to store the translated content
en.php :
<?php
$lang = array(
"lang-selector-href" => "<a href='index.php?lang=fr'>",
"lang-selector-switch" => "Français (French)",
...
...
);
?>
fr.php :
<?php
$lang = array(
"lang-selector-href" => "<a href='index.php?lang=en'>",
"lang-selector-switch" => "English (anglais)",
...
...
);
?>
Question:
How could i could I change the link to:
<a href='current-page.php?lang=fr'> Français (French) </a>
or:
<a href='current-page.php?lang=en'> English </a>
according to the set language?
here is my config.php file
<?php
session_start();
// if no language selected go for english
if (!isset($_SESSION['lang']))
$_SESSION['lang'] = "en";
else if (isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang'])) {
if ($_GET['lang'] == "en")
$_SESSION['lang'] = "en";
else if ($_GET['lang'] == "fr")
$_SESSION['lang'] = "fr";
}
// load content
require_once "content/languages/" . $_SESSION['lang'] . ".php";
?>
Sorry if it sounds very basic, i'm sure it is but couldn't find a simple answer that would work...
Thank you!
Make Dropdown list in html which options have language value like below as,
<select onchange="callSomeFunc()">
<option value="fr">French</option>
<option value="en">English</option>
</select>
On change of dropdown list you need to call function which will set selected language value in to php $_SESSION.

PHP if/then statement

Need help with some php to modify a WP plugin which is paid memberships pro. Terrible at php here.
What am trying to do is create one line of code that would say if the membership level equals XXX then print this link. SO the variable I would need are somewhere in this line I imagine:
<li><strong><?php _e('Membership Level', 'paid-memberships-pro' );?>:
</strong> <?php echo $current_user->membership_level->name?></li>
The above is just a snippet of code already found in the page I want to create this if/then link statement.
so something like:
<?php if($Membership Level == $Conflicts of Interest #14124(that's the name
of one level) then print this link.
AM I making sense?
Edit:
Thanks to some help below, this seems to work:
<?php if($membership_level == 'Conflicts of Interest #14124') {
echo "<a href=\"conflicts-of-interest-in-modern-legal-practice-and-internal-
investigations-14124/\">Testing</a>";
}
?>
But the 'Conflicts of Interest #14124' doesn't match even though it is the correct name.
Then general If else statement in the html page
<?php if($membership_level == 'string to be compared') {
echo 'the link that you want to print if it is a string. you can replace this string
with a variable if the value is in variable'
} else {
'any other text if require else you can remove the else block'
}
?>
Hope that helps.

Detect browser preferred language to serve html pages [duplicate]

This question already has answers here:
Detect Browser Language in PHP
(16 answers)
Closed 8 years ago.
I'm a designer with little coding knowledge outside of html/css. Reading on the 'net I find many references to some php using http_accept_language but cannot find a step-by-step "do this, do that" explanation and consequently I don't know what to do with / where to put the code that people present for the job.
I'm developing a site with an English main home page. There, users are presented with a choice of two branches. One branch will be only in English and the other in English plus 3 Scandinavian languages. The non-English pages are pre-translated, not created on the fly by Google.
I want that after arriving at the main home, a user who opts to go into the 4-language section will be initially served a page in the language of their browser (on all pages there will be always be a menu to manually select another language, if preferred).
Is this a job for php? If so I'd be grateful for some code and help in how to use it.
You can use HTTP_ACCEPT_LANGUAGE header. Save supported languages to array and then loop the header using foreach.
$languages = array('en', 'fi', 'sv', 'no');
$header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($header as $lang) {
if(in_array($lang, $languages)) {
header("Location: $lang.php"); // i.e. fi.php or se.php
break;
}
}
HTTP_ACCEPT_LANGUAGE contains something like this:
Accept-Language: en-gb,en;q=0.5
As you can see, there is multiple languages, en-gb and en. That's why it's clever to loop the header. If you prefer functions, here's one:
function get_user_lang() {
$languages = array('en', 'fi', 'sv', 'no');
$header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($header as $lang) {
if(in_array($lang, $languages)) {
return $lang; // i.e. fi.php or se.php
break;
}
}
}
echo 'Your language is ' . get_user_lang();
Step-by-step guide:
Create new files for each language. Name them like this: "fi.php" or "se.php".
Place the first code part to very top of your home page. That file must include .php ending, so it must be php file. If you don't understand, that should be where to place it:
<?php
$languages = array('en', 'fi', 'sv', 'no');
$header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($header as $lang) {
if(in_array($lang, $languages)) {
header("Location: $lang.php"); // i.e. fi.php or se.php
break;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>title</title>
</head>
<body>
contents
</body>
</html>
Navigate to your home page in your browser. If your browser's language is english, it'll redirect to "en.php", if swedish; "se.php" and so on.
You can see all language codes from this link, swedish is "sv".
If you have the INTL extension you can use Locale::acceptFromHttp().
Here's the example from the PHP documentation.
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo $locale;
$locale will be something like "en_US". If you only want the language and not the country, use something like this:
$locale = explode('_', Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
echo $locale[0];

Internationalisation language switch?

I am using the internationalisation block for basic page translation between Spanish and English. By default the content is in Spanish. When I change the language to English the page content is translated, however the autonav appears blank. I have used page CID's to determine the language differences. In my header.php file for my theme I am using the follow statement:
if(!$c->getAttribute('english_menus')) {
$lh = Loader::helper('section', 'multilingual');
$lang = $lh->getLanguage();
$bt = BlockType::getByHandle('autonav');
$bt->controller->displayPages = 'custom';
if ($lang == "en_EN"){
$bt->controller->displayPagesCID = 166; //English section cID
} else {
$bt->controller->displayPagesCID = 171; //Spanish section cID
}
$bt->controller->orderBy = 'display_asc';
$bt->render('templates/top_nav/view');
}
This displays the top level navigation fine. however when the country flag is selected the auto nav menu disappears and does not display in english
Many Thanks
Do you mean to have the
if (! $c->getAttribute('english_menus')) {
at the top?
It doesn't make much sense to me. Any chance your english page (or whatever page you land on after the country flag) has that attribute set to something?

How to display a message on joomla homepage only?

How can I display a message only on the homepage of joomla?
I am interested in displaying it on site.com and not site.com/index.php/page or anything else then site.com.
I have tested the following:
<?php $app = JFactory::getApplication();
$menu = $app->getMenu();
$lang = JFactory::getLanguage();
if ($menu->getActive() == $menu->getDefault($lang->getTag())) : ?>this is the homepage
<?php endif; ?>
and this
<?php $menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo "this is the homepage";
}
?>
The problem is that I can still see the message "this is the homepage" on pages like http://site.com/index.php/category/id/78-article which clearly isn't the homepage. It seems that whenever there is index.php in the link, the above code thinks it belongs to the homepage.
This has nothing to do with the 'index.php' in the link. Instead it is related to the fact that the link at http://site.com/index.php/category/id/78-article does not have a menu item associated with it. To do exactly what you are wanting, you will probably need to get a little trickier with the code and check to make sure that the actual page's information matches the homepage information:
$jinput = JFactory::getApplication()->input;
$menu = & JSite::getMenu();
$active = $menu->getActive();
$default = $menu->getDefault();
if (
$active == $default &&
$jinput->get('option') == $default->query['option'] &&
$jinput->get('view') == $default->query['view'] &&
$jinput->get('id') == $default->query['id']
) {
echo "This is the homepage";
}
I am checking the default menu items option (which component) and view and id values against those set in the input.
http://site.com/index.php/category/id/78-article This link will set the id to 78 and likely change the view and option from what it is defined as in the menu for the homepage, so the trigger will not occur.
OK, just an idea but try:
if (preg_match('/index\.php$/',$_SERVER['PHP_SELF'])) {
echo "this is the homepage";
}
You should try this.
1. Check the menu in which has been assigned "Home"(This is the homepage) from the administrator section. Copy the link shown over there.
It would be something like
index.php?option=com_somecomponent&view=someview
Now Goto your index.php in template.
Write a condition there
if(JRequest::getVar('option')=='com_something' && JRequest::getVar('view')=='someview'){ //show message }
This should do the trick for you !! But keep in mind. If you change the menu of homepage, this will have to be changed accordingly.

Categories