We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33968
    • 863 Posts
    I have a snippet that sends emails to users, run by a cronjob at set intervals so no current context applies.

    The thing is, I need to be able to access lexicons in different languages so English and French users get text in their respective languages.

    How do I do it in php? This is roughly what I am doing:
    foreach ($users as $user) {
        $modx->lexicon->load($user->get('lang').':namespace:topic');
        $msg[] = $modx->lexicon('key');
    }
    

    But the second language to load is the one that is used thereafter regardless of the language required.

    Is there any way to do this using the api or do I need to manually include both lexicon files, load them into arrays and then access the strings by language + key? [ed. note: okyanet last edited this post 12 years, 5 months ago.]
      • 33968
      • 863 Posts
      Really, I would just like to know if this is possible at all with the current state of Revo - before I go ahead and do something inefficient smiley

      What if I dynamically set the culture key within the loop... will try that out. Edit: didn't work.

      Otherwise, feature request perhaps? [ed. note: okyanet last edited this post 12 years, 5 months ago.]
        • 3749
        • 24,544 Posts
        Do you know whether it's the load that's failing or getting the user's language?

        I'd use a different variable name for $user, though I don't think it will solve your problem.
          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
          • 33968
          • 863 Posts
          Hi Bob, thanks for the reply.

          I'm not doing exactly what I've shown above, it was just to illustrate the problem. There are no issues with the rest of my code and the load works fine - but it doesn't seem to be possible to load two languages at once.

          In the same way, if you try to load two lexicon languages using the tag syntax within a resource it will still only make one language available:
          [[%key? &namespace=`ns` &language=`en`]]
          [[%key? &namespace=`ns` &language=`fr`]]
          

          This will results in the string repeated in one language only, rather than the first in English and the second in French.

          As an example of where this might be an issue (not related to my problem), how would you make the following language choice menu using lexicon tags:

          English - Deutsch - Francais - Italiano - Espanol

          You'd need to grab the language name from each lexicon language right? [ed. note: okyanet last edited this post 12 years, 5 months ago.]
            • 3749
            • 24,544 Posts
            Only one lexicon language can be in memory at a time for use in a snippet, though recalling load() each time through the loop should work, in theory. OTOH, loading them manually and using str_replace would probably be faster.

            I'm surprised that it doesn't work with tags though. I'm almost certain that multiple language tags worked for me when I tested it for the book, though that was a while ago.

            I was using my phone before, so let me ask my question more clearly:

            In your foreach loop, what you describe could be caused by $user->get('language') always returning the same thing, or by load() ignoring the language argument. I was asking if you'd checked to see which was the case.
              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
              • 33968
              • 863 Posts
              Quote from: BobRay at Dec 01, 2011, 07:31 PM
              Only one lexicon language can be in memory at a time for use in a snippet, though recalling load() each time through the loop should work, in theory.
              That's what I'd figured, and as my lexicon file is quite small (just for the emails) it wouldn't really have been a huge problem.


              I'm surprised that it doesn't work with tags though. I'm almost certain that multiple language tags worked for me when I tested it for the book, though that was a while ago.
              Just tested again and the last language to be specified is the one that is used for all lexicon tags.


              In your foreach loop, what you describe could be caused by $user->get('language') always returning the same thing, or by load() ignoring the language argument. I was asking if you'd checked to see which was the case.
              $user->get('language') is working correctly, and in fact load() is not ignoring the argument either. Here's another test I did:
              $languages = array('en','fr','en','fr','en');
              foreach ($languages as $lang) {
                $modx->lexicon->load($lang.':test:default');
                $output .= $modx->lexicon('test.small')."\n";
              }
              return $output;
              

              Which outputs:
              Small
              Petit
              Petit
              Petit
              Petit
              

              So load() is working fine but the last 'new' language to load in is the one that stays in memory.

              Actually I can easily find a workaround but it would be very useful if there was a way to do it natively - I'll file a feature request unless anyone jumps in with an obvious solution smiley
                • 3749
                • 24,544 Posts
                Did you try uncached language tags?

                _
                [[!%key]] 
                  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
                  • 33968
                  • 863 Posts
                  Yeah, tried uncached tags but I think the problem is that only one language file can be loaded into memory so there's no apparent way to access two languages.

                  Have filed a feature request here:
                  http://bugs.modx.com/issues/6204
                    • 28215
                    • 4,149 Posts
                    This issue is fixed in Git/nightlies, and will be in 2.2-rc2.
                      shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                      • 33968
                      • 863 Posts
                      That is excellent news, many thanks Shaun!