Results 1 to 4 of 4

Thread: Define vs $lang array vs gettext for language?

  1. #1
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default Define vs $lang array vs gettext for language?

    Hi guys

    I am looking at a couple of scripts I have and each one uses language files differently and I was just curious why one way is better than the other.

    In one we have a language file using defines, e.g.

    Code:
    define ("L_FAQ","FAQ");
    The next uses arrays such as

    Code:
    $lang['FAQ'] = "FAQ";
    and the third uses language in the content of the files like

    Code:
    echo _("FAQ")
    The first two enable you to edit the one language setting that is then applied throughout the entire site where the define or array is used. However the third one requires you to do a search and replace in all files if you want to change the language text in that echo to be the same throughout the site.

    So what is better and why?

    Cheers

    GW
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    1. Defined constants:
    Constants are terrible because they are constant-- their values cannot change. So if in the middle of your script you needed to redefine one of these, it would be impossible. And if you name another one (even accidentally) the same name, then you will get a big error. But since in this case you are working with data that probably won't change (during each page load), that shouldn't be too much of a problem.
    One big advantage of this is that these values are global so you never need to worry about whether a phrase is available within a function (though this can be solved other ways). Also, this will create a separated system from variables, so your language system won't interfere with the rest of the code.
    One major problem here is that constants aren't complex at all: you can't make an array, you can organize them, etc. There's no much you can do with them except echo them directly. Also, defining is a fairly long operation compared to $var = 'value';. If you do this a lot, that might get annoying. There's also no advantage: it requires the same quotes, etc. And when outputting these, it might be a little hard to read because constants don't always distinguish themselves that well compared to text output within your code.


    2. This is the best way and I don't really have any comments about it. You can even use a multi-level array like $lang['home']['nav']['go'] = 'Go';
    I like this, and I use it. Of course it's problematic having a lot of data floating around, so only load the files you need, but there's no real way to avoid this aside from using a database instead of variables/constants in your file.
    This can be convenient for organizational purposes since you can do a lot with arrays. You can also loop through arrays in case you get something like $lang['mypage']['errors'].
    Many sites just use a single level of array, sometimes even with arbitrary names like $lang[1] and $lang[393]. And others use more descriptive names (but keep them simple).
    I personally like using a multi-level array, and that also helps with organizational pieces as mentioned above. I use $lang for everything and give each phrase set a name, so $lang['set']['term'].


    3. I have no idea what this syntax is. I've never seen it before. Can you explain a little? I don't know how to look it up either since it's just symbols.



    I do, however, actually use functions in my system. Here's a basic summary:
    1. I use an array called $lang like in (2) above.
    2. I store the phrases in PHP files and include() them to load the phrases. This is very efficient, and the only problem would be a security risk or misplaced comma if you have others editing your files. Educate them or look over the phrases yourself before using them on a live site.
    3. I created a function called lang() that automatically echoes a value from lang. So lang('page.value') will echo $lang['page']['value']. This is very complex to setup (since it allows for infinite layers) and it is probably a lot more than you need on your site, unless it's a large scale site specifically about languages, as mine is. But this gives you an idea of how to expand if you need it.


    Another unrelated possibility, in addition to using a database, is that you could create a class Lang and make an instance $lang. Then set the language, do $lang->translate('phrase') and other things. However, since the vast majority of your work will be just using the translate method, I prefer the much more direct: <?php lang('phrase'); ?> which is the shortest completely valid syntax I've seen for this. <?=$lang['phrase']?> is also used, but in addition to being ugly (I think), it's not valid on many PHP configurations. (This is called "short tags".)


    In summary, there are many ways this can be done and it really doesn't matter except in efficiency and until your site gets big enough (in terms of server load and phrase management) how you do this. But once that does happen, the details may matter. So if you have a fairly small site, don't worry too much and use whatever you'd like. But if this is a large scale site, you may want to consider the various options more carefully.

    The most efficient way may be a database, but that gets very complex and doesn't allow the easy method of storing phrases in files which is popular especially if others are translating. (You could always import from files into the database though.)


    Let me know if you have any more questions, but in some ways this is just your decision.

    Personally I'd strongly recommend working with arrays if possible, or a database if you find that better. The other methods don't really fix anything (what's wrong with arrays anyway?) and they have some downsides.

    Creating a function system that will manage all of this for you is nice so you can always change things later. For example, if I decide to change my language system to a database (instead of PHP files) then all I need to do is modify my lang() function, and then all instances of lang('section.phrase') will be automatically updated. For this reason alone, any direct usage is a bad idea.

    For my site, I added a number of convenient things, such as term replacement (for example, "Welcome, %U" is for "Welcome, Username") and also whether you want to immediately echo or instead return the value of the language phrase, such as if you are storing the value for later use.
    This is all handled within the lang() function.

    Then of course you'll need to figure out how to manage all of this. I've built a fairly complex and useful language admin system on the site where users with the right privileges can edit phrases.


    There's a lot that needs to go into any site with translations and complexity, so if you can keep it simpler do that. My site must be built on a very complex system so it can do everything I needed. For example, I've almost finished creating my own wiki software because none of the ones I found were able to handle multi-language (interactive) wikis. Mediawiki, for example, created a separate database/wiki for each language, though it could (somewhat) interact with each instance.

    So in conclusion-- it took me a long time to setup the framework for my site to allow so much. If that sounds desirable and you have the time (and hopefully no deadline), it will give you the best result. But if not, keep things simple and use any method that works. But also don't cut any corners to the point where you need to recode each page-- that's never fun.


    EDIT: regarding (3) above-- I looked at gettext() on php.net and this may be what you are referring to. What I've described above is similar in complexity (I think), and if gettext() works for you there's nothing wrong with that. However, it is something extra to learn, and there's no reason to use it if you don't need it. Personally I like the control of writing it myself (since I know exactly what everything is doing and can change little details), but I've heard about this (in passing) and it seems to be effective too.


    Finally, an important note:
    If what you have above is all for research, then you can ignore this. If, however, those are components you may wish to use in your site, consider this:
    The method using $lang or the more complex version I described will allow you to interact with any of the others. If you bring in a component to your site that uses define(), then you can do it like this: define('PHRASE',$lang['phrase']);. You never need to really use anything aside from $lang.
    While it may be possible to do this with any of the methods, since $lang gives you the most control, I don't see a good reason not to use it.
    Last edited by djr33; 07-09-2010 at 02:40 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    gwmbox (07-09-2010)

  4. #3
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default

    Daniel, excellent post thank you very much. 2 is my preferred option however I have on a couple of occasions been told gettext() is better??? Personal pref I think but like you I like the control of 2

    Cheers and thanks

    GW
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Either way will work. gettext will require understanding more to use it, and end up with a lot of (but not complete) control. $lang won't require much to start using it, but it will require a lot of work to build a complete system, but you'll have complete control.

    My site is built around this idea of changing languages, so for me making my own made sense. Honestly, if you can avoid it it might be best-- if gettext works for you (and it probably would) it might save you some time.

    But whatever you pick, just choose something you like working with. I like my version and that's all that really matters. If you like it, go ahead, or if you think another might be better there's no reason not to use it.

    Also, there may be other prebuilt solutions out there like my version and gettext... really they're doing basically the same thing, just with different approaches.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •