We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37747
    • 50 Posts
    I set up my modx multi-language sites with each language in a folder, the parent resource is like a "language config" page, it has all the translations for general stuff used in the templates eg. "book now" for buttons, IDs of resources that need to be referenced like "ID of the offers page for this language".

    I then have a custom snippet I wrote that checks the parent resource, grabs the TVs and outputs them in a placeholder. Here's the snippet (called [[languageConfig]] btw):

    function getConfigTvs($parent) {
    	global $modx;
    	$tvs = array();
    	$resource = $modx->getObject('modResource', $parent);
    	$tvs = $resource->getMany('TemplateVars');
    	foreach($tvs as $tv) {
    		$key = $tv->get('name');
            $value = $tv->getValue($parent);
    		$modx->setPlaceholder('translate.'.$key, $value);
    	}
    }
    $parent = $modx->runSnippet('UltimateParent');
    getConfigTvs($parent);
    


    In the template you just use [[+translate.bookNow]] where bookNow is a tv with some value specified in the parent resource and make sure you have called the snippet at the top of the template. Now, this all works fine most of the time except occasionally all the placeholders are empty. If I log into the manager and clear the cache all the placeholders are back to normal again, if I use modx with the resource caching turned off this never happens but of course this is not a viable solution for production. I have tried calling the snippet uncached but this doesn't seem to make any difference seeing as it's the resource level caching that seems to cause the issue.

    Oh, and in case anybody notices....yes this pretty much the same as using:
    [[getResourceField? &id=`[[UltimateParent]]` &field=`bookNow` &processTV=`1`]]
    

    ...just a lot shorter markup in the template....so potentially what I need to do is figure out how to make a new snippet call out of getResourceField that always looks at ultimateParent, always processes TVs and just takes a tv name as an option? Seeing as getResourceField works way better than my custom snippet!

    Any advice much appreciated.

    ps. I have this caching issue on separate sites both locally (MAMP) and online
      • 4172
      • 5,888 Posts
      not sure, if this does fix it:

          $tvs = array();
          $resource = $modx->getObject('modResource', $parent);
          $tvs = $resource->getMany('TemplateVars');
          foreach($tvs as $tv) {
              $key = $tv->get('name');
              $value = $tv->getValue($parent);
              $modx->setPlaceholder('translate.'.$key, $value);
          }
      
          return '';


      [[languageConfig? &parent=`[[UltimateParent]]`]] 


      maybe its a good idea to have a unique snippet-tag for each different output

      see also
      http://rtfm.modx.com/display/revolution20/Internationalization
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 37747
        • 50 Posts
        Hi Bruno,

        Thanks for the quick reply, so you think that the problem could be with calling ultimateParent from within the snippet? I will try your solution and see.

        Also I didn't understand this:
        maybe its a good idea to have a unique snippet-tag for each different output
        Please could you elaborate?

        I did have a look at both Lexicons and different contexts per language but I can't see either solution working. Lexicons because they are just not user friendly for the client to edit per language. You see it's the client that builds the new languages. I give them a site with 1 language and they are able to duplicate and translate this folder as many times as they need with no input from me. All languages use the same templates and also don't have to follow the exact same structure ie. blog only in English.

        Context settings was almost good enough but...if I have a site with 7 languages and I need to add a new translation placeholder then I would have to add it to each of the 7 contexts so not really viable either. What would have been great is global settings that have context specific overrides.

        So at the moment this is the best solution I found but I am keen to explore other options.

        Thanks for your help.