Jump to content

Recommended Posts

I'm completely new to this.. but whats the best way to have my site in avaliable languages?

 

+ Bonus question.. is putting HTML code in designer mode something worth doing? Like.. if I had language selectors, alot of the wording will be in other languages. There for.. they'll be defined by variables in that language file. So all the words.. will be php tags. So it'd be better off the whole site being in php, so designer mode is just.. pointless?

Link to comment
https://forums.phpfreaks.com/topic/199304-language-selector/
Share on other sites

For languages, most sites use templates and inside the templates are defined variables for different messages. These variables come from a database or an alternative file which has them defined. You use the same variable name, it is just defined differently depending on which file has been included or which language has been selected from the database.

 

As far as "designer mode" no clue what you are talking about.

Link to comment
https://forums.phpfreaks.com/topic/199304-language-selector/#findComment-1046037
Share on other sites

For languages, most sites use templates and inside the templates are defined variables for different messages. These variables come from a database or an alternative file which has them defined. You use the same variable name, it is just defined differently depending on which file has been included or which language has been selected from the database.

 

As far as "designer mode" no clue what you are talking about.

Dreamweaver.. the designer mode is only for html/css, if most the site ends up being.. echo's or whatever.. itd be rather pointless... right?

 

I understand what your saying about languages, it's what I thought. Though how would it be set to choose a particular language for the user?

Link to comment
https://forums.phpfreaks.com/topic/199304-language-selector/#findComment-1046040
Share on other sites

Set it in the database for the user and have a dropdown menu, which is dynamically generated from the "Available language" files or database entries. They select it you save it and just reference it when you pull the user data and include / pull that data from database / file.

Link to comment
https://forums.phpfreaks.com/topic/199304-language-selector/#findComment-1046042
Share on other sites

What happens if the user isn't logged in, or registered.. is there a way to have a language in a cookie.

value of the cookie lets say is.. english

I put the value of the cookie, to a variable.. $lang

Then do something similar to this..

include("templates/languages/language.$lang.php");

 

I'll have like.. a select menu on the footer.. which.. onchange is an auto submit?

Link to comment
https://forums.phpfreaks.com/topic/199304-language-selector/#findComment-1046049
Share on other sites

The use of a PHP file to hold your translations is a bad idea because whenever you have user-contributed content you will have mutliple sources to maintain (file + database table) this problem further expands whenever the client asks for more functionality (more database tables) for which also translations exists.

 

1 guarantee = CHANGE

 

Another bad idea is the use of so-called placeholders like TXT_FRP_MAIN whenever an error in translation occurs (missing file, translation) the user will see these placeholders making for an ugly looking unusable website.

 

What is a better approach? 1) The use of a single database table that will hold all translations for your application also future additions 2) The fallback language acts as an identifier for all other languages and guarantees a usable website at any point.

 

<a href="#"><?php print translate('Signup, it is free!'); ?></a>

 

In this scenario when the user language detection fails shall the user be served with the default language leaving the website in a usable format.

Link to comment
https://forums.phpfreaks.com/topic/199304-language-selector/#findComment-1046064
Share on other sites

So if the use of "place" holders is bad, how do you display the translation coming from the database?

 

The only bad part about it, is you are under the assumption that if he incroperates a language and forgets to fill out all the language translations of the item added, which, of course would display the empty translation, IF, and only if, you were a moron and did not code for this. It should be entered into the DB as a "no translation error". You are also under the assumption that by placeholder I meant a CONSTANT, which of course I did not specify. I was more under the lines of getTranslation('field-name')  which could handle this "mishap" elegantly, in that if the fetched translation is empty or not specified it gives a default message or something similar.

 

As far as from the file to the database, I would agree that the database would be the favored result, but files work just as well. They just require an effort to make changes by editing / uploading the file, where as the database you can create the interface to add / modify textual data.

Link to comment
https://forums.phpfreaks.com/topic/199304-language-selector/#findComment-1046094
Share on other sites

Here's my two cents:

 

From your description I am thinking this is a primarily static site that you want as multi-language.

 

Go ahead and use "designer" mode or whatever you are comfortable with to create the pages initially. Then, replace each phrase word with a snippet of PHP with a single echo statement. That way the file is still in a human "readable" format. Example:

ABC Company<br />
<p><?php echo $companyDescription; ?></p>
<br />
<p><?php echo $contactLabel; ?><br />
<?php echo $phoneLable; ?>: 1-800-123-4567<br />
<?php echo $AddressLable; ?>: 123 Main St., Anywhere, ST 12345<br />
</p>

 

You will then need a script to determine the user's language and you could call it with an include at the top of every page. Here is a script I have used before:

// Function: getLanguage($languageList [, $selectedLang] [, $defaultLang])
//
// Parameters:
//   - $languageList: (Required) An array of all available languages for the user
//   - $selectedLang: (Optional) The language the user has selected (GET or POST)
//   - $defaultLang:  (Optional) the default language to use if unable to determine
//                               user's language from seleted or saved values.

function getLanguage($languageList, $selectedLang=null, $defaultLang=null)
{
    //Set the default value (or first option in $languageList)
    $userLanguage = (!in_array($defaultLang, $languageList)) ? $languageList[0] : $defaultLang;

    //Detemine selected/saved user language
    if (!is_null($selectedLang) && in_array($selectedLang, $languageList))
    {

        //Request was made to change the language
        $userLanguage = $selectedLang;
        setcookie('language', $userLanguage, time()+3600*24*365); //Expires in 1 year
        $_SESSION['language'] = $userLanguage;
}
    else if (isset($_SESSION['language']) && in_array($_SESSION['language'], $languageList))
    {
       //There is a saved language value in the SESSION data
        $userLanguage = $_SESSION['language'];
    }
    else if (isset($_COOKIE['language']) && in_array($_COOKIE['language'], $languageListAry))
    {
        //There is a saved language value in the COOKIE data
        $userLanguage = $_COOKIE['language'];
        $_SESSION['language'] = $userLanguage;
    }

    //return the user's language
    return $userLanguage;
}

 

Once the user's langauge has been determined you can get the appropriate content to create the variables to populate the page. As Permiso stated thsi can be done from a database or a flat-file

Link to comment
https://forums.phpfreaks.com/topic/199304-language-selector/#findComment-1046098
Share on other sites

So if the use of "place" holders is bad, how do you display the translation coming from the database?

 

I didn't say that placeholders are bad I said that placeholders like TXT_FRP_MAIN are bad.

 

It should be entered into the DB as a "no translation error"

 

No, your end result should be that in any circumstance your website remains usable for the end-user. "no translation error" means nothing for your end-users.

 

I was more under the lines of getTranslation('field-name')  which could handle this "mishap" elegantly, in that if the fetched translation is empty or not specified it gives a default message or something similar.

 

Something like?

 

<a href="#"><?php print getTranslation('Signup, it is free!'); ?></a>

 

Is perfectly fine as in the worst case scenario it print's "Signup, it is free!" and your website remains albeit usable.

 

As far as from the file to the database, I would agree that the database would be the favored result, but files work just as well.

 

Sure and in a (static) application this is probably the optimal choice (however experience learns that these do not stay static for long). Be prepared for CHANGE. And a single database table is just perfect for that as it can contain all translations for all components.

Link to comment
https://forums.phpfreaks.com/topic/199304-language-selector/#findComment-1046105
Share on other sites

mjdamato, example how I call the function?

Do I need any other pages with all the variables for the languages in..

 

I'm really confused :(

 

The function I provided is only used to determine the correct language to display to the user. I included the list of parameters and what they are for. Plus, the comments should be enough to understand what it does. Below is the sample code I have as an example of how to use it. But, again, it only determines which language to show to the user. It is then up to you to decide on the functionality you will use to disply the appropriate content based upon the language selected. You will need some functionaliy on the site for the user to select/chnage their language. This should pass a GET or POST var whcih is used by the function.

 

<?php
//Example usage
//
//Create list of available languages
$languages = array ('Spanish'=>'sp', 'French'=>'fr', 'English'=>'en');
//Get the language to show the user
$currentLanguage = getLanguage($languages, $_GET['lang'], 'en');
//Create vars for illustrative purposes
$sessionLang = (isset($_SESSION['language'])) ? $_SESSION['language'] : 'Not set' ;
$cookieLang = (isset($_COOKIE['language'])) ? $_COOKIE['language'] : 'Not set' ;
?>
<html>
<body>
Current language: <?php echo array_search($currentLanguage, $languages) . " ($currentLanguage)"; ?>
<br /><br />
$_GET['lang']: <?php echo $_GET['lang']; ?><br />
Session language: <?php echo $sessionLang; ?><br />
Cookie language: <?php echo $cookieLang; ?><br /><br />

Change Language
<?php
foreach ($languages as $name => $value)
{
    echo "<a href=\"?lang={$value}\">{$name}</a> ";
}
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/199304-language-selector/#findComment-1046113
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.