On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Previously i used a dedicated document to store all of the translated strings for the site so YAMS could pull them in using &get=`tv` syntax. But as the number of strings (headings, copyright, etc.) grew managing them as tvs became cumbersome. Is there a way to use translation files similar to MODx locale files with YAMS.
    I noticed that there’s a &get=`text` option but according to the manual the &from=`` should contain text itself not a path to a file. Also unfortunately Wiki leaves out the description of the proper syntax for the text.



    • virtualbear Reply #2, 8 years ago
      hi,

      in order to work with lang files and yams i’ve written a little snippet called langfile
      <?php
      require( MODX_BASE_PATH . 'assets/snippets/langs/lang_'.$langId.'.php' );
      return htmlentities($zilang[$text]);
      ?>

      as you may have guessed have my lang files in a assets/snippets/langs directory, the langfile are called after YAMS langid, lang_fr.php, lang_en.php and so on

      in the lang files i just set an array()
      $zilang = array(); //not to conflict with a potential othe $lang var, but call it as you want :)


      and then
      $zilang['bonjour'] = "bonjour"; //in lang_fr.php
      $zilang['bonjour'] = "hello";   //in lang_en.php


      and, in the page, where you need it
      [!langfile? &text=`bonjour` &langId=`(yams_id)`!]


      though it may seem heavy to require the lang files many times in a page, honestly it is not as, being just an array to play with, php does this fingers in the nose smiley

      i’ve tried other methods, for example using a snippet that parses the right lang file and then set placeholders for each word, of course, it seems smart as you only need to call the snippet once on top of the templates and then use [+bonjour+] in the body where needed but it is actually a bit heavier as you set a lot of placeholders you do not need when choosing an array index is really not memory gready

      you could even write a plugin to do this onparsedocument but, once more, to my mind, it is heavier than a simple array parsing

      have swing
        réfléchir avant d'agir
      • simple and elegant.
        thanks virtualgadjo for sharing