<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>gHacks Technology News &#124; Latest Tech News, Software And Tutorials &#187; php constants</title> <atom:link href="http://www.ghacks.net/tag/php-constants/feed/" rel="self" type="application/rss+xml" /><link>http://www.ghacks.net</link> <description>A technology news blog covering software, mobile phones, gadgets, security, the Internet and other relevant areas.</description> <lastBuildDate>Sat, 11 Feb 2012 21:54:04 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/> <item><title>Creating a simple multi-lingual website</title><link>http://www.ghacks.net/2009/03/19/creating-a-simple-multi-lingual-website/</link> <comments>http://www.ghacks.net/2009/03/19/creating-a-simple-multi-lingual-website/#comments</comments> <pubDate>Thu, 19 Mar 2009 16:54:33 +0000</pubDate> <dc:creator>Daniel Pataki</dc:creator> <category><![CDATA[Web Development]]></category> <category><![CDATA[languages]]></category> <category><![CDATA[php]]></category> <category><![CDATA[php constants]]></category> <category><![CDATA[translation]]></category> <guid
isPermaLink="false">http://www.ghacks.net/2009/03/19/creating-a-simple-multi-lingual-website/</guid> <description><![CDATA[If you want to create a simple webpage for yourself, listing who you are, how you can be contected and what you do, you might want to add a few languages in there. You could use Google Translate, but that does not yield the best (and professional) results, so it would be best to translate [...]]]></description> <content:encoded><![CDATA[<p>If you want to create a simple webpage for yourself, listing who you are, how you can be contected and what you do, you might want to add a few languages in there. You could use Google Translate, but that does not yield the best (and professional) results, so it would be best to translate the page yourself, or have someone do it for you.</p><p>Now, you could have different pages like &#8220;about_english.php&#8221; and &#8220;about_spanish.php&#8221;, but this would make updating a pain, and very inflexible, plus if you have 20 languages, it means 20 files per page. So how do we get around this? Let me introduce you guys, to PHP constants.</p><p>A constant is defined just like a variable, but as its name suggests, it is in fact constant. It can be echoed just like a variable, and is great for defining set pieces of text. Let me show you my method of using constants to easily keep multiple language versions of a webpage, without having to have multiple files for each page.</p><p><span
id="more-11335"></span></p><p>First of all, let&#8217;s take a look at how to define constants. Constant names are uppercase strings, but must not begin with a number and should not start with a special character. For example, let&#8217;s define a constant:</p><p>define(&#8220;NAME&#8221;, &#8220;Daniel Pataki&#8221;);</p><p>In this exampe we have defined a constant called &#8220;NAME&#8221;, and gave it a value of &#8220;Daniel Pataki&#8221;. I gave the constant a describing name, so I know that this constant holds my name.</p><p>The way I create multiple languages is that I create 1 language file for each language. I name them according to the standard 2 letter convention. The English file is named &#8220;en.php&#8221;, the French would be &#8220;fr.php&#8221; and so on. I include the language file the user needs at the beginning of all my other files containing text, so they will automatically be defined. There are many way to approach this, but for now, let&#8217;s say that the page is always in English, unless a user clicks one of the language links. So at the beginning of the code I would write:</p><p>&lt;?php<br
/> if (isset($_GET['lang']))<br
/> include($_GET['lang'].&#8221;.php&#8221;);<br
/> else<br
/> include(&#8220;en.php&#8221;)<br
/> ?&gt;</p><p>This way the relevant file will be included only. Each file contains the list of definitions I need in the same structure. My name in the English version is &#8220;Daniel Pataki&#8221;, but in Hungarian we put our family names in the front, so in &#8220;hu.php&#8221; I would define it as:</p><p>define(&#8220;NAME&#8221;, &#8220;Pataki Dániel&#8221;);</p><p>In the page&#8217;s code, I can then simply type the following to display my name:</p><p>&lt;h2&gt;&lt;?php echo NAME ?&gt;&lt;/h2&gt;</p><p>This would show my name as a level 2 heading. If the user is viewing in English, the output would be &#8220;Daniel Pataki&#8221;, if the user is viewing in Hungarian it would display &#8220;Pataki Dániel&#8221;, since this time &#8220;hu.php&#8221; is included, and not &#8220;en.php&#8221;. Notice that when displaying contants you do no need to put any special characters before or after the constant name, just type the constant itself as you defined it.</p><p>You can use this method to create languages for larger sites too. The reason I especially like this is that it is quite easy to translate the site, you just need to send the file over to someone and he will be able to do it easily, without any training. If you have a larger site it might be a good idea to indicate where the constant will be used. You can do this by defining a constant name like &#8220;SIDEBAR_COMMENTS&#8217;, or &#8220;CONTACT_NAME&#8221;. This way you and your translators will have an easier time, especially if you also use PHP comments in the language file for further pointers.</p><p>&lt;em&gt;If you liked this article, perhaps you&#8217;d like to take a look at &lt;a href=&#8221;http://scriptastique.com&#8221;&gt;Scriptastique&lt;/a&gt;, which is a blog (and upcoming tutorial site) aimed at professional and aspiring coders. We just started, but there are daily posts and the tutorials (along with screencasts) are on the way!&lt;/em&gt;</p> ]]></content:encoded> <wfw:commentRss>http://www.ghacks.net/2009/03/19/creating-a-simple-multi-lingual-website/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
