Results 1 to 3 of 3

Thread: [php]How to: Make a language system(basic)

  1. #1
    A12Alex is offline Newbie Net Builder
    Join Date
    Feb 2009
    Location
    Queensland, Australia.
    Posts
    90
    Blog Entries
    1
    Thanks
    15
    Thanked 9 Times in 6 Posts

    [php]How to: Make a language system(basic)

    Now... I'm kinda bored right now and I want to program / write a tutorial so why not write a language system? It's simple, it's effective and it saves you time in the long run(Well... it doesn't save you any time persay but it does save the time of the person viewing the page, which in cause could cause the person to return and / or tell other people about said page / site).

    First off, I must explain to each and every one of you, that this tutorial will teach you a simple way of allowing your content to be read in multiple languages, without having the reader to translate it using a translator(Which takes time and most readers mightn't bother).

    I highly recommend you don't put the following into practice on a blog or any content that is dynamic really(referring to dynamic content pulled from a database). I will be using arrays and echos in this tutorial(It's as simple as that), so you should at least have a basic understanding of arrays.

    Now that we have that out of the way, say this is the page that we want the language system to work with or for(Before you say anything the page is meant to be very simple):

    PHP Code:
    <html>
    <
    head>
    <
    title>
    Example page
    </title>
    </
    head>
    <
    body>
    <
    h1>This is just an example page</h1>
    <
    p>If this really was a page(That was going to be or is being used on a live siteI'm sure you would have at least a fair share of content on said page</p>
    <!--
    I told you it was going to be a simple page(no doctype :/)
    -->
    </body>
    </head>
    </html> 
    Now, I'm going to say that when said page was made we didn't say it as a .php file so, we are going to have to change the file type(use mod_rewrite to not damage incoming links from search engines and such) to a .php and then either make a new directory and call it say... lang or you could(I wouldn't recommend it) you skip past this step. Now that you have made a new directory(or didn't) make two new files called english.php and german.php(Couldn't think of another language).

    Now We have set up pretty much all you need, it's time to really get into the programming side. In english.php place the following code inside of the file:

    PHP Code:
    <?php
    $lang 
    = array(
    't1' => 'Example page',
    'h11' => 'This is just an example page'//h11 refers to the first usage of the h1 tag
    'p1' => 'If this really was a page(That was going to be or is being used on a live site) I'm sure you would have at least a fair share of content on said page',
    '
    c1' => 'I told you it was going to be a simple page(no doctype :/)'
    );
    ?>
    If you haven't all ready guessed all ready that code I just gave you won't work. Why? simple, I didn't escape any apostrophes('), to escape apostrophes simple put a backslash (/) before the apostrophe('), why do you have to do this you ask? simple again, the backslash will tell PHP that you want the apostrophe to be used within the string and NOT to be used to end arrays string. Now go a head and add those backslashes in(what? I'm not going to do all the work). Also, I won't really explain the arrays as you should all ready know how this array will work if you have a pretty basic understanding.

    Now for the second language file, german.php. It's pretty much the same set up but this time rather then have the text in English... it's going to be in German. D :

    PHP Code:
    <?php
    $lang 
    = array(
    't1' => 'Beispiel Seite',
    'h11' => 'Das ist nur ein Beispielseite',
    'p1' => 'Wenn das wirklich war eine Seite (die war zu werden oder wird auf einer Liveseite) Ich bin mir sicher, dass Sie mindestens einen gerechten Anteil von Inhalten auf Seite gesagt',
    'c1' => 'Ich hab doch gesagt, es ging um eine einfache Seite (keine DOCTYPE: /) '
    );
    ?>
    And that's pretty much the language files done(not prefect German but whatever... I only took German for one semastors at school). Now that the language files are done it's time to move back to the page that we set up before and add some php so that it will work with our language files.

    We are going to get the text like so: $lang['tagnumber']; I am going to use short tags(oh my) to echo the text, but that will cause comaptilbity issues with a few servers so yeah... change it to <?php echo $lang['tagnumber']; ?> rather then <?=$lang['tagnumber'];?> k? but before we add any of that to the file we need to pre define, what language the page will be in(remember this is just a basic tutorial), if you don't know about defineing things in php click here.


    PHP Code:
    <?php
    define
    ("PDL""english"); // set the predefined language for the page(I wouldn't include this if I where to make use of some other things but yeah... again.. this is just a basic tutorial)
    $pdlv PDL;
    require_once 
    '/lang/'.$pdlv.'.php';
    ?>
    <html>
    <head>
    <title>
    <?=$lang['t1'];?>
    </title>
    </head>
    <body>
    <h1><?=$lang['h11'];?></h1>
    <p><?=$lang['p1'];?></p>
    <!--
    <?=$lang['c1'];?>
    -->
    </body>
    </head>
    </html>
    (Now before you say it(or say it again), I know the comments aren't really a must have in codes(refering to there language) but I felt like adding it in anyway.)

    And that's pretty much it really, I haven't had time to spell check it or re read it so there may be mistakes, they will be fixed up once I get back. Also, please share your thoughts on this tutorial. : Dcompatibilitydefiningreferring
    Last edited by A12Alex; 11 February, 2009 at 08:45 AM.

  2. #2
    jane0109 is offline Unknown Net Builder
    Join Date
    Feb 2009
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post
    that's good, thanks fr the reference, but is it ok to see the output of that? do you have any url or site which refer to your post?

  3. #3
    A12Alex is offline Newbie Net Builder
    Join Date
    Feb 2009
    Location
    Queensland, Australia.
    Posts
    90
    Blog Entries
    1
    Thanks
    15
    Thanked 9 Times in 6 Posts
    I'll put it up on a domain I'm currently working on / with. (oh my... my first proxy : D)

Similar Threads

  1. Replies: 1
    Last Post: 1 April, 2011, 10:13 AM
  2. Basic forms of ownership
    By toyo123 in forum Business
    Replies: 0
    Last Post: 5 September, 2009, 11:00 AM
  3. Replies: 2
    Last Post: 11 July, 2009, 07:56 AM
  4. Basic SQL Tutorial (DML)
    By A12Alex in forum Databases
    Replies: 0
    Last Post: 13 February, 2009, 07:05 AM
  5. Replies: 5
    Last Post: 15 January, 2009, 00:15 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •