We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29076
    • 615 Posts
    In modx you can do things in two ways, the complex way, for people with higher scills than I have, and the simple way, which for me is the best (only) way. And that is some of the things that makes modx genius.

    So, my problem (question):

    My wish is to be able to use namespaces (values) for different languages from Lexicon in snippet-calls in different templates. Example:

    In template baseTemplate_eng, or on a page that use my baseTemplate_eng-template, I want to use this call [[SimpleSearch &use_namespace-value_for_the_english_part_(en)_for_this_snippet_in_Lexicon_manager]].

    In template baseTemplate_nor, or on a page that use my baseTemplate_nor-template, I want to use this call [[SimpleSearch &use_namespace-value_for_the_norwegian_part_(no)_for_this_snippet_in_Lexicon_manager]].

    Is there a way to do this? (Sorry if this is obvious, I couldn't find it by searching the forums, stackoverflow or other places.)

    Thanks.
      I think, thererfor I am! But what I am, and why...?
      • 3749
      • 24,544 Posts
      You can use full language string tags:

      [[%LanguageStringKey?
         &language=`en`
         &namespace=`NameSpaceName`
         &topic=`TopicName`
      ]]
      


      As an alternative, you can use regular language tags with just the key and create a custom snippet to put in the template that will load the appropriate lexicon:

      $modx->lexicon->load('language:namespace:topic');
      


      That will always overwrite the current values with those from the files you specify.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 29076
        • 615 Posts
        Quote from: BobRay at Jan 06, 2014, 08:36 AM
        You can use full language string tags:

        [[%LanguageStringKey?
           &language=`en`
           &namespace=`NameSpaceName`
           &topic=`TopicName`
        ]]
        
        Thanks! I got that part working alright. What I miss is the simple way of telling a snippet which language I want to use, like this: [[SimpleSearch &lang=`en`]] or [[SimpleSearch &lang=`no`]].

        But I don't know if that's possible. I guess it can be done in some way with Contexts, but for me Contexts is unfortunately too complicated, and I just don't need that complexity. Not for this site.
          I think, thererfor I am! But what I am, and why...?
          • 45063
          • 40 Posts
          Try to create a TV with language value: en and no. And then, call you lexicon with &language=`[[*tv]]`.

          Or, if you sites have a different addresses, you can specify language by HTTP host:

          if ($modx->context->get('key') == 'mgr') {
                  return;
          }
          
          switch ($_SERVER['HTTP_HOST']) {        
          case site.com:
          $MySetting = $modx->newObject('modSystemSetting');
          $MySetting->set('lang', 'en');
          $cacheRefreshOptions =  array( 'system_settings' => array() );
          $modx->cacheManager-> refresh($cacheRefreshOptions);            
             break;
          case site.no:
          $MySetting = $modx->newObject('modSystemSetting');
          $MySetting->set('lang', 'no');
          $cacheRefreshOptions =  array( 'system_settings' => array() );
          $modx->cacheManager-> refresh($cacheRefreshOptions);
             break;
          
          }
            • 45063
            • 40 Posts
            Or, use your lexicon string INSIDE the snippet. I guess, MODX allows it.
              • 29076
              • 615 Posts
              Thanks for answering, but in my case here I don't want to use Contexts. And the language settings should be placed within the snippet-call in the template, not via a TV.

              I realize now that this problem can not be solved without using Contexts, or reprogram the SimpleSearch snippet, so I will instead present it as a request in another post:

              A new default-function in modx-snippets: use &lang=`` to load the corresponding lang-strings from Lexicon without the use of Context.
                I think, thererfor I am! But what I am, and why...?
                • 3749
                • 24,544 Posts
                What I miss is the simple way of telling a snippet which language I want to use, like this: [[SimpleSearch &lang=`en`]] or [[SimpleSearch &lang=`no`]].

                How do *you* know what language you want the snippets to use? If it's the URL they come in on, setting a $_SESSION variable would be one way. You could also make it a User Setting (if they're logged in), or a cookie. Many snippets have a &language property, even though it's often undocumented.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 45063
                  • 40 Posts
                  Better to use lexicon calls inside the snippet itself, and pass the language into them with some kind of TV, because, I assume, the snippet is used on two different pages (with different languages)?
                    • 3749
                    • 24,544 Posts
                    Right, but the OP is talking about existing MODX snippets that don't have that capability.

                    That said, many of those snippets (e.g. getResources) use Tpl chunk and you could put language tags in those chunks.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 45063
                      • 40 Posts
                      Quote from: BobRay at Jan 07, 2014, 08:16 AM
                      Right, but the OP is talking about existing MODX snippets that don't have that capability.

                      That said, many of those snippets (e.g. getResources) use Tpl chunk and you could put language tags in those chunks.

                      SimpleSearch snippet? I looked on a github page of this snippet and there is lexicons present, actually. The code:

                      $this->modx->lexicon->load('sisea:default');


                      For language change, create a TV with language parameter (en/no) and before this line, you can add something like this:

                      $page = $modx->getObject('modResource', 123); // number of resource that uses this TV
                      $lang = $page->getTVValue('lang'); // TV named 'lang'
                      $Setting = $modx->getObject('modSystemSetting', 'cultureKey');
                      $Setting->set('cultureKey', $lang);


                      Finally, insert [[!SimpleSearch]] snippet on page Uncached.