pedro84 Posted October 24, 2006 Share Posted October 24, 2006 Hello again!I'm PHP neewbie, but I'm writing my first website, with huge help from good people. I have problem. I want to make multilingual site based on lang files[code]define ('LOGIN', "Login");[/code]But I don't know how to start, I found some articles over the internet, but I don;t understand them:)Greetings, Pedro Juan Alvaredo-Kowalski [Spanish-Polish Surname:)] Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/ Share on other sites More sharing options...
xsist10 Posted October 24, 2006 Share Posted October 24, 2006 index.php[code]<?phpdefine("LANGUAGE", "EN");require_once("include/language/". LANGUAGE .".inc");echo "<h1>". WELCOME_MESSAGE ."</h1>";?>[/code]EN.inc[code]<?phpdefine("WELCOME_MESSAGE", "Welcome to the site");?>[/code]Then based on what LANGUAGE is set to, it will load a difference language file. Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113529 Share on other sites More sharing options...
pedro84 Posted October 24, 2006 Author Share Posted October 24, 2006 Thank You.But how to make lang switcher? Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113532 Share on other sites More sharing options...
heckenschutze Posted October 24, 2006 Share Posted October 24, 2006 Usually, an array of strings is used, since constants cannot be redefined.eg:EN,[code]<?php$language["welcome"] = "Welcome";?>[/code]DE,[code]<?php$language["welcome"] = "Hallo"; // lols i dunno german :P?>[/code]then use something like:[code]<?phpdefine("LANGUAGE", "EN");require_once("include/language/". LANGUAGE .".inc");echo $language["welcome"];?>[/code]HTH! Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113533 Share on other sites More sharing options...
xsist10 Posted October 24, 2006 Share Posted October 24, 2006 You can do the language selector like so[code]<?php$lang = addslashes($_GET["lang"]);require_once("include/language/". $lang .".inc");echo $language["welcome"];?><form action='index.php' method='get'><select name='lang'><option value='EN'>English</option></select><input type='submit' value='Change Language'></form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113535 Share on other sites More sharing options...
pedro84 Posted October 24, 2006 Author Share Posted October 24, 2006 [code]<?php$lang = addslashes($_GET["lang"]);require_once("include/language/". $lang .".inc");echo $language["welcome"];?><form action='index.php' method='get'><select name='lang'><option value='EN'>English</option></select><input type='submit' value='Change Language'></form>[/code]I use this for test and i get error [code]Warning: main(include/language/.inc): failed to open stream: No such file or directory in c:\usr\apache\httpd\html\index.php on line 3Fatal error: main(): Failed opening required 'include/language/.inc' (include_path='.') in c:\usr\apache\httpd\html\index.php on line 3[/code]I've got files made.BTW Have I put this code :[code]<?php$lang = addslashes($_GET["lang"]);require_once("include/language/". $lang .".inc");echo $language["welcome"];?>[/code]in all my pages? Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113538 Share on other sites More sharing options...
heckenschutze Posted October 24, 2006 Share Posted October 24, 2006 xsist10's method is prone to URL injection.Think about it, what if ?lang= was defined as ../../../veryimportantpasswordfile or somesuch... ?Better off to employ swtiches...eg,on all your pages:[code]<?phprequire_once("langman.php");if(!SelectLanguage($_GET["lang"])) die("Error selecting language!");echo $language["welcome"];?>[/code]then in langman.php:[code]<?php$language = array();function SelectLanguage($strLang){ global $language; $inc = "en"; switch(stripslashes($strLang)) { case "de": $inc = "de"; break; case "en": $inc = "en"; break; default: $inc = "en"; } include_once("languages/" . $inc . ".inc"); if(isset($lang)) { $language = $lang; return true; } return false;}?>[/code]Then in ./languages/en.inc for example:[code]<?php$lang["welcome"] = "welcome";?>[/code]and ./languages/de.inc :[code]<?php$lang["welcome"] = "hallo";?>[/code]If your unsure how to apply this, just ask ;)Then to select the language, just use mypage.php?lang=de on ALL your pages, where you are using language, if ?lang is not defined, english will be used.hth. Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113548 Share on other sites More sharing options...
pedro84 Posted October 24, 2006 Author Share Posted October 24, 2006 Thank You.I apply what You write and I get blank page.What did I make wrong? Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113555 Share on other sites More sharing options...
heckenschutze Posted October 24, 2006 Share Posted October 24, 2006 Haha, I spelt welcome wrong :s, sorry Try it now... :D Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113558 Share on other sites More sharing options...
pedro84 Posted October 24, 2006 Author Share Posted October 24, 2006 It works now. Thank You veerry much. You are verny nice human!But tell me one thing...how to select language??:>:) Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113561 Share on other sites More sharing options...
heckenschutze Posted October 24, 2006 Share Posted October 24, 2006 Well, the method above is simple but can become confusing in large sites... (Since ?lang= has to be appended to every link)...Ideally, you should store the users language in a session...Meaning on the top of every page you would place:[code]<?phpsession_start();require_once("langman.php");if(!SelectLanguage($_SESSION["lang"])) die("Error selecting language!");echo $language["welcome"];?>[/code]Then setlang.php :[code]<?phpsession_start();$strLang = $_GET["lang"];$_SESSION["lang"] = stripslashes($strLang);header("Location: index.php"); //** redirect to where u want...?>[/code]Then!, create links to change the language:[code]<a href="setlang.php?lang=de">German</a><br /><a href="setlang.php?lang=en">English</a>[/code]Tell me how that pans out... :)hth. Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113565 Share on other sites More sharing options...
pedro84 Posted October 24, 2006 Author Share Posted October 24, 2006 t works ;D ;D ;DThank You very muuuch!!I'm sure I will have problem later:) I'll post here ok?:) Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113569 Share on other sites More sharing options...
heckenschutze Posted October 24, 2006 Share Posted October 24, 2006 Yeah just make a new topic... :PNo probs :P Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113572 Share on other sites More sharing options...
pedro84 Posted October 24, 2006 Author Share Posted October 24, 2006 I knew it:)When I put it on my site, it works, but i get this error:[code]Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at c:\usr\apache\httpd\html\index2.php:2) in c:\usr\apache\httpd\html\index2.php on line 3[/code] too.Whats wrong?:>:) Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113582 Share on other sites More sharing options...
heckenschutze Posted October 24, 2006 Share Posted October 24, 2006 [url=http://php.net/session_start]session_start()[/url]; MUST be called before any output is sent to the browser (eg spaces or HTML), [url=http://php.net/session_start]session_start()[/url] should idealy be the topmost thing in your website. The manual also mentions this :)hth. Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113584 Share on other sites More sharing options...
pedro84 Posted October 24, 2006 Author Share Posted October 24, 2006 OMG...what a asshole:) I'm:)Thank YouYou know what? You are veeery kind to me:) Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113587 Share on other sites More sharing options...
redbullmarky Posted October 24, 2006 Share Posted October 24, 2006 [quote author=heckenschutze link=topic=112507.msg456745#msg456745 date=1161689759][url=http://php.net/session_set]session_set()[/url]; MUST be called before any output is sent to the browser (eg spaces or HTML), [url=http://php.net/session_set]session_set()[/url] should idealy be the topmost thing in your website. The manual also mentions this :)hth.[/quote]session_set doesnt exist as a function, but otherwise you're right. it's [url=http://www.php.net/session_start]session_start[/url] instead.cheers Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113588 Share on other sites More sharing options...
heckenschutze Posted October 24, 2006 Share Posted October 24, 2006 Im kind to everyone... im told ;)No probs helping... First time ive written a multi-lingual system :P [quote]session_set doesnt exist as a function, but otherwise you're right. it's session_start instead.[/quote]Sorry, my bad, just got back into php after a year off (CGI instead):P Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113590 Share on other sites More sharing options...
pedro84 Posted October 24, 2006 Author Share Posted October 24, 2006 One question:I incude some scripts with this code:[code]<?phpif (isset($_GET['op'])){switch ($_GET['op']) {case 1: include("content1.php"); break;case 2: include("content2.php"); break;case 3:include ("sendtofriends/sendtofriends.php"); break; default: include("news/show_news.php"); // this stops pageid to be set by the user trying to break your script}} else {include ("news/show_news.php"); //this one allows for just index.php to get default page} ?>[/code]Can I make them mulitlingual the same way? Or make each module lang file?Thanks in Advance Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113626 Share on other sites More sharing options...
True`Logic Posted October 24, 2006 Share Posted October 24, 2006 basically youll need to include a file which has an array of the english word into the other language, then split your dialogues into an array on chr(32), after that you only need to use str_replace($words[$num], $lang[$words[$num]]) in a while statement =) Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113674 Share on other sites More sharing options...
pedro84 Posted October 24, 2006 Author Share Posted October 24, 2006 Could You write clearly for me? ??? ??? 8) Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-113794 Share on other sites More sharing options...
pedro84 Posted October 27, 2006 Author Share Posted October 27, 2006 Someone know how to apply it in some scripts? Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-115538 Share on other sites More sharing options...
pedro84 Posted October 27, 2006 Author Share Posted October 27, 2006 Does anyone knows? Quote Link to comment https://forums.phpfreaks.com/topic/24908-multilingual-page-problems/#findComment-115595 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.