On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi,
    I've tried googling this, but I feel like I'm not familiar enough with modx and its terminology to really understand the search results. Hopefully some of you can tell me if what I'm looking for is possible. (Also, I haven't gotten around to installing modx yet due to hosting issues, so please excuse the vagueness.)

    I'd like to build a site where there is a front-end langauge switch (like an English and a Danish flag in the upper right corner e.g.). I've read something about the lexicon, which I assume I could use to change the menues and such?
    But how do I make it so that, when the user creates a new post (document) in the manager, there'll be both a Danish and an English content field (preferably right next to or below eachother)? I'm not looking for an automatic translation, but instead the option to write our own translation as well as being able to post different things depending on language chosen (since not everything is relevant to non-Danish speakers).

    Is there any extras that does this (and if so, which ones)?
    • I think migxmultilang extra does something similar from what I remember - but it can be a little tricky to get it to play ball also from what I remember.

      You can also setup multiple contexts to handle different languges (lookg at XRouting extra) but that would require creating essentially two different sites as new resources/posts aren't automatically carried over to the other context(s).
        ■ email: lk@alienbuild.uk | ■ website: https://alienbuild.uk

        The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
      • I thought there were tools that could provide a rough machine translation of content?

        I have made two identical sites in different languages before, and its important when you get started that you make the collation of your database utf-8, otherwise problems could/will arise
        • I'm pretty sure there's no extra for what you want.

          Having only two languages makes it easier. Personally, I would avoid using multiple contexts for this.

          I think I would set this up with the Resource 'content' field your primary language and a Template Variable (TV) for the secondary language. It might be easier to let the users create the posts in NewsPublisher in the front end. That would make it easy to show the two text areas one above the other, leave out fields you don't care about, and let you set the captions for each textarea.

          Assuming that the TV is named "secondaryLanguage" -- this is one way to do it:

          Create two System Settings with these keys: primary_language, secondary_language and either en or dk for the values.
          Create a namespace called multilang with a core path of {core_path}components/multilang/
          Create a directory in the core/components/ directory called multilang

          Where the content will be displayed on the page, put this in the template:

          <div class="content_div">
              [[!ShowContent]]
          </div>
          

          Then create a snippet called "ShowContent with this code:
          /* ShowContent snippet */
          
          
          $primaryLanguage = $modx->getOption('primary_language');
          $secondaryLanguage $modx->getOption('secondary_language');
          
          $lang = $modx->getOption('site_language', $_SESSION, $primaryLanguage, true];
          
          if ($lang == $primaryLanguage) {
              $output = '[[*content]]';
              $modx->lexicon->load($primaryLanguage .  . ':multilang:default');
          } else {
              $output = '[[*secondaryLangauge']]';
              $modx->lexicon->load($secondaryLanguage . ':multilang:default');
          }
          return $output;
          


          The lexicon->load() line is to load language strings for other parts of the page or template that should be in a particular language. Use language tags for them and create two lexicon files containing the values. The two files would both be called default.inc.php and would be in the en and dk directories under core/components/multilang/lexicon.

          core
             components
                multilang
                   lexicon
                      en
                        default.inc.php
                      dk
                        default.inc.php
          


          Your two language selection buttons can input buttons for forms that submit to the current page and contain a hidden input that sets the language. You'll need a snippet at the top of the body section that sets the 'site_language' $_SESSION variable to the value submitted in the form.

          $_SESSION['site_language'] = 'en';
          or
          $_SESSION['site_language'] = 'dk';


          If your users log in, you could set a user setting in that snippet with the key 'site_language' and modify the code of the ShowContent snippet to use that if it exists.

          If this sounds like too much for you, check out MigxMultilang (MML). Here are the official docs for it: https://github.com/Bruno17/migxmultilang. From what I read, MML is very robust. It creates multiple TVs for you (one for each language) and embeds the two-letter language code in the URL. I'm not sure it would handle creating user settings for you, but if users don't log in, they won't work anyway -- and as long as the users come in on the correct URL (e.g., yoursite.com/dk/somepage) the'll get the correct language.

            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
          • @BobRay - Thank you! I'll try this.

            Edit: What if I create a TV for the English bit and then create a snippet that displays either the regular resource content -or- the info in the TV (depending on the chosen language). Could something that simple might work?
            • @Erica that sounds right to me
              • @Erica -- That should work fine as long as you have a reliable way for the snippet to know what language is in effect (e.g., the $_SESSION['site_language'] value).

                That would work best like this:

                $defaultLanguage = 'en'; // or 'dk'
                $lang = $modx->getOption('site_language', $_SESSION, $defaultLanguage, true); 


                Using getOption() this way handles the cases where the $_SESSION variable is not set or is empty. In both cases, the default language will be set.

                BTW, that whole plan of mine above was off the top of my head late at night, so I can't guarantee that it will all work. wink
                  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