We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25481
    • 5 Posts
    Hi,

    I am new to Modx, and it takes me quite a while to get used to it (I’m used to Drupal, Joomla and the likes) but sofar so good.

    I need to add multiple language support to my site. I am using the wiki document http://wiki.modxcms.com/index.php/How_to_make_Multi_Lingual_Site

    I created the snippets "redirect", "languages" and the variable "languages". Then it says in the wiki: "Edit your templates to call the Languages snippet with no parameters."

    Now ... I have read all about snippets, I get the point more or less, and searched the forum, but can’t find the answer to the following (probably amazing stupid) question:

    What does "Call the snippet" mean and how do I do that??

    I want to have the flags for the language selection in the top of my sidebar, I added [[languages]], but nothing happens. (also tried [*languages*] and all sorts.

    Note, I am familiar with PHP, and for a test have successfully added a PHP snippet called currentyear
    echo date('Y');
    which works fine anywhere in the template. I have also developed a similar multi language site in plain php, but I want to delve deeper into Modx.

    I am testing this locally using a WAMP server on the latest MODX version 0.9.5 running PHP 5.1.4, MySQL 5.0.22

    What am I missing, it must be something silly, but it’s driving me nuts! I am also developing the same site "online", but wish to test this locally first (and I don’t want to be considered spamming my website)

    thanks in advance,
    Ronald
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Think of a snippet as being a php function; for example your snippet could have
      return date('Y');
      

      every snippet should end with a return statement to close it, even if it’s just a bare return; statement.

      "Calling" a snippet means putting its [[SnippetName? &option1=`var1` ...]] tags in your html code somewhere.

      If it’s a dynamic snippet, such as handling the results from a form, or your date snippet, the [[...]] syntax won’t work, since the snippet’s output will be cached along with the rest of the document. Calling it [!...!] will prevent that, and will evaluate the snippet’s code fresh for every page request, even if the page itself is cached.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 25481
        • 5 Posts
        Thanks for your quick respons.

        the current_year snippet works just like this:

        snippet current_year:
        <?php
        echo date('Y');
        ?>


        and in my template I have something obvious:
        <p>All contents © [[current_year]] ronald<p>


        it outputs nicely: All contents (c) 2007 ronald

        however, the language snippet builds/returns a string $output (the flag-gifs associated with the languages (en and de) and href’s to the appropriate url’s for those languages.

        how do I use/include that string? the wiki also says: "to call the Languages snippet with no parameters"

        I think the TV languages, referred to in line 4 might have something to do with it?

        here’s the language-snippet (taken from the wiki);

        <?php
        // get languages info
        $tv = $modx->getTemplateVar('languages', "", $modx->documentIdentifier);
        $languages = $tv['value'];
         
        if (!strlen($languages))
        {
          return "";
        }
         
        $langnames = array(
          "en" => "English",
          "de" => "Deutsch"
          );
         
        $output = "";
         
        // get entries in languages list
        $entries = explode(",", $languages);
        // loop through language entries
        for ($e = 0; $e < count($entries); $e++)
        {
          // seperate into lang code and target document id
          list($lang, $targetid) = explode("=", $entries[$e]);
          $image = '<img src="assets/images/'.$lang.'.gif" width="16" height="11" border="0" alt="'.$langnames[$lang].'" />';
          if (strlen($output))
          {
            $output .= ' ';
          }
          $output .= '<a href="'.$modx->makeUrl($targetid).'">'.$image.'</a>';
        }
         
        return $output;
        ?>


        thanks again,
        Ronald

        edited a bit later, I worked through the rest of the manual, and something seems to be happening, not perfect, but I think I’m getting there, I’ll give you an update soon!
          • 25481
          • 5 Posts
          Well, I managed to do this for 1 page, however if I want the same snippet to work for ALL pages, I still run into problems.

          The wiki documentation provides an example for 1 page, and then states:

          "For each page on your site you need to specify which is the translated page. This is achieved using the languages template variable. The format of the template variable value is:

          two_letter_iso_country_code=document_id,two_letter_iso_country_code=document_id "


          and a bit futher

          "To add more languages:

          1. Duplicate all site pages in the new language
          2. Add the new flag icon to assets/images
          3. Add the new language to the array in the Languages snippet
          4. Edit existing pages to specify where the new translated page is, using the languages template variable

          For a menu system, use the Wayfinder snippet with the following call:

          [!Wayfinder? &startId=`[[UltimateParent]]`!]

          and each webpage always lists the menu from the top folder of each language.. "


          This is not clear enough (to me as a noob at least!). Mind you, I do appreciate the snippets provided!

          Is the basic functionality of this snippet to provide multi language support for only ONE page, or should it work across the whole site?

          I am "guessing" I need to set the TV languages to e.g.

          TV "Languages":
          en=2,de=3,en=5,de=6,en=8,de=9


          meaning that the German version of pageid 2 is pageid 3, English page 5 is German page 6, English page 8 is German page 9 etc. etc.

          Evaluating the TV, in this case this will result in 6 links (and their flags) in the sidebar, as the TV languages is processed for each entry, something like this (not actual code):

          Result from Snippet "Languages":
          <a href="page-2" img src="en.gif"> 
          <a href="page-3" img src="de.gif"> 
          <a href="page-5" img src="en.gif"> 
          <a href="page-6" img src="de.gif"> 
          <a href="page-8" img src="en.gif"> 
          <a href="page-9" img src="de.gif"> 
          


          This is obviously not what I want, I guess the TV "languages" should be global as far is the number of languages and flags w/links is concerned, yet "per page" for each translated page.

          Also not clearly documented is how the document structure should be for multiple pages, say I have a page "Home", an page "About" and a page "Products". Each should be in both English and German

          I am "guessing" again:

          "root"
          pageID 1 (Home - redirects to default language "en", page 2)
          pageID 4 (About - redirects to default language "en", page 5????)
          pageID 7 (Contact - redirects to default language "en", page 8????)
          ¦
          +--en
          ¦   +--PageID2 (Home in en)
          ¦   ¦
          ¦   +--PageID5 (About in en)
          ¦   ¦
          ¦   +--PageID8 (Contact in en)
          ¦    
          +--de
          ¦   +--PageID3 (Home in de)
          ¦   ¦
          ¦   +--PageID6 (About in de)
          ¦   ¦
          ¦   +--PageID9 (Contact in de)
          ¦


          And on top of that, some more questions:
          - How to get the top menu multi language? Is that included?
          - Will the redirects take into account the previously selected language, e.g. user starts from (deafult) English Home page (ID 1), then selects German Home (ID 3), navigates to About (ID 4), will that redirect to ID 5 (Default English, according to the redirect in the "root" page, or to ID 6, German according to the TV and the previously selected language)?
          - what about "latest news" or "blog entries" flashes in the sidebar?

          Anyway, I think the concept is fine, but could be better explained (again, maybe just for a noob!) by simply adding an example for more than 1 page.

          But again, I really appreciate all the work that’s been done, I am trying to implement this, but feel that I should either better understand it, or write my own snippet, probably having 1 TV for the available languages (2 in this case), and 1 TV for the redirects (with 3 entries in this case). It is vital that I can implement this in an easy way.

          Thanks for your comments, and apologies for my ignorance and being a pain ;-)

          Ronald
            • 25481
            • 5 Posts
            Ok, the content of template variable IS actually per page, so it’s working pretty neat!

            Will have to sort out the menu now, but getting there.

            Like I said, forgive me my ignorance smiley