Harmen

Import different languages from DB

Recommended Posts

Hey all,

For my website I had to make a connection with an external DB. In that DB there are categories of products in different languages, and I want to import these categories in the languages that are available in processwire, I can do that with a very long code but I want to make it shorter. So I wrote a function to get the categories, but the different languages of the category names not, only if I insert the ISO-code hard-code. So here is my code:

protected function get_category()
	{   $languages = wire("languages");
		try {
			$prestashp = $this->get_dbPrestashop();
			foreach($languages as $language) 
				{
					$iso_code= $language->name ;
							
						$categories = $prestashp->query(
							"SELECT l.id_lang, l.iso_code, cl.description, cl.name AS category_title, cl.id_shop, c.active, c.id_category, c.id_parent, c.position, cl.link_rewrite
							FROM ps_category c
							INNER JOIN ps_category_lang cl
							ON c.id_category=cl.id_category
							LEFT JOIN ps_lang l
							ON cl.id_lang=l.id_lang
							WHERE c.active='1'
							AND cl.id_shop='1'
							AND c.id_parent = '2'
							ORDER BY c.position
							");
				}
		}catch(Exception $e) {
			$this->message('Failed to show categories. Try again later.');

			return false;

		}
		return $categories;
	}

 

So in the DB there is Deutsch, Dutch, English(default), Spanish, Italian. And when I edit the template the different languages of the category names need to be imported in the right tab of the field.  52a3549a8adaed2d5f44cac6dfb6d9d3.png
https://gyazo.com/52a3549a8adaed2d5f44cac6dfb6d9d3 As you can see, there are language tabs above the field, and the names of the category in that language need to be insterted there. 

Anyone have an idea how I can fix this?

Share this post


Link to post
Share on other sites
3 hours ago, Harmen said:

I'm not really used to hooks, maybe there is another option?

I don't think @Zeka was saying you have to use hooks - I think he was showing you the code that soma used for saving a language specific value to a field.

Using:

"name$lname" where $lname is actually defined as the ID of the language.

Take another look at soma's code and I think you will find what you need.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By RichyRich
      Okay folks, I am trying to put a character limit on a body of text. I am using the following function, which is place in the _func.php file:
      function wordLimiter($str, $limit = 120, $endstr = '…'){ $str = strip_tags($str); if(strlen($str) <= $limit) return $str; $out = substr($str, 0, $limit); $pos = strrpos($out, " "); if ($pos>0) { $out = substr($out, 0, $pos); } return $out .= $endstr; }  
      This works just fine on it's own. When placed in Hanna code I get a fatal error, "Call to undefined function wordLimiter()". Code in hanna
      <?php echo "<div class=\"grid_xs-1\">"; foreach ($pages->find("template=property") as $properties) { //attempt at defining $summ to call wordLimitor() $summ = wordLimiter($properties->body); echo " <div class=\"col-5 card property\"> <a href=\"{$properties->url}\"> <img src=\"{$properties->images->first->size(500,500)->url}\"> </a> </div> <div class=\"col-7\"> <h3>{$properties->title}</h3> <p>{$summ}</p> </div> <div class=\"col-12 grid details\"> <div class=\"col\"> <i class=\"fa fa-dollar\" aria-hidden=\"true\"></i> {$properties->price}</div> <div class=\"col\"> <i class=\"fa fa-bed\" aria-hidden=\"true\"></i> {$properties->bedroom}</div> <div class=\"col\"> <i class=\"fa fa-bath\" aria-hidden=\"true\"></i> {$properties->bathroom} </div> <div class=\"col\"> <i class=\"fa fa-arrows\" aria-hidden=\"true\"></i> {$properties->square_feet} SqFt</div> </div>"; } echo "</div>";
      I have also tried placing the function wordLimiter() at the top of the Hanna but it give a different error, " Cannot redeclare wordLimiter() "

      Am I missing something here? Limitation somewhere?
    • By creativejay
      I'm displaying a list of products which are found by their templates, but the pages are taking a very long time to load. At first, I blamed it on my image rendering (using PIM2), but even with all those images now stored in the file tree, the page is taking abysmally long to load. ProCache seems to help but I don't feel as though what I'm trying to do should be gnawing the bones of my resources quite so long.
      The variable for the selector is defined in my header include:
      $productCatList="prod_series|prod_series_ethernet|prod_series_access|prod_series_accessories|prod_series_fiber|prod_series_pwr_supplies|prod_series_pwr_systems|prod_series_wireless"; $getCurrentProdOptions="template=$productCatList, prod_status_pages!=1554|1559|1560|4242"; Then in the template for the page upon which the directory loads:
      $products = $pages->find("$getCurrentProdOptions"); include_once("./prod-list-row.inc"); echo $out; And the prod-list-row.inc foreach (which is on every page that's exhibiting the slowdown):
      <?php $sum = 0; $out =""; $out .= "<div class='span_12_of_12'>\n"; foreach($products as $p){ $sum += 1; if ($sum % 2 == 0) { $bgcolor = '#fff'; } else { $bgcolor = '#e4e4e4';} $par = $p->parent; $out .="<div class='section group' style='background: $bgcolor ; min-height: 110px'>\n"; $img = $p->prod_image; $thumb = $img->pim2Load('squarethumb100')->canvas(100,100,array(0,0,0,0),'north',0)->pimSave()->url; $out .="<div data-match-height='{$p->title}' class='col span_2_of_12 hide'>"; $out .="<a href='{$p->url}'><span class='product-image-box'><img src='{$thumb}' alt='{$p->title}' title='{$p->title}'></span></a>"; $out .= "</div>"; $out .= "<div data-match-height='{$p->title}' class='col span_6_of_12'>"; $out .= "<div class='prod-list-name-label'><a href='{$p->url}'>{$p->title}</a></div>"; if($page!=$par) { $out .= "<div class='prod-list-category-label' style='font-size: .7em;'>Category: <a href='{$par->url}'>{$par->title}</a></div>"; } $out .= "<div class='list-headline' style='font-size: .8em;'>{$p->headline}</div>"; $out .="<div class='learn-more-buttons-sm'>"; $out .="<a href='{$p->url}' title='Product Specs and Documentation'><span class='find-out-more-button' style='font-size: .8em;'><i style='font-size: .8em;' class='fa fa-lightbulb-o' ></i> &nbsp; Learn More</span></a>"; $out .="</div>"; $out .="</div> \n"; $out .= " <div data-match-height='{$p->title}' class='col span_4_of_12'>"; if(count($p->prod_feat_imgs) >0 ){ $out .= "<div class='featured-icons-list' margin: 2em .5em;'>"; foreach($p->prod_feat_imgs as $feat){ $icon = $pages->get("$feat->prod_featicon_pages"); if($icon->image) { if($feat->prod_feat_textlang) { $icontitle = $feat->prod_feat_textlang;} else {$icontitle = $icon->title;} $out .= "<img src='".$icon->image->size(35,35,$imgOptions)->url . "' alt='" . $icontitle . "' title='" . $icontitle . "' class='listing-feat-icon' style='margin-right: .5em;' />"; } } $out .= "</div>"; if($p->prod_product_line){ foreach($p->prod_product_line as $pline) if($pline->image) { $out .= "<div style='height: 35px;'>\n"; $out .= "<img src='{$pline->image->size(75,35,$imgOptions)->url}' alt='{$pline->title}' />"; $out .= "</div>"; } } } $out .= "</div>"; $out .="</div>"; } $out .= "</div>";  
      Is there a clear culprit here of what I'm doing that's so stressing the system?
      I turned off TracyDebugger because I saw another thread about that causing slowdown (even though I'm using the latest), but that had no effect. Every time I thought I found the culprit and commented it out, nothing changed.
      Would appreciate some more eyes on this. Thank you!
      ETA: prod_feat_imgs is a repeater field which contains a Page reference field (from which I pull the image and title) and a multilanguage textfield (to override the page reference title if it exists). Could that be the problem?
    • By prestoav
      Hi all,
      A very odd issue this one!
      I'm working on a site with 5 languages - English (default), French, German, Spanish and Polish. I have a set of pages set up as a country list (templae='country') which contain various country info like two-letter country code etc and where the page title is the Country name. This is so that I can use the country list in various parts of the site.
      When the site is showing in English, Spanish and Polish I can display the country list without an issue. However, when the page is displaying in French or German the country pages seem not to exist. So, this code...
       
      $countries = $pages->find("template=country"); foreach ($countries as $country) { if ($country) { echo $country->title . "<br />"; } } ...displays the country list just fine in English, Spanish and Polish but displays nothing in French and German.
      Just for extra info, the Title field is set up as PageTitleLanguage and all languages are setup the same.
      Any ideas?
      TIA.
    • By Erbenos
      Hello,
      recently i came across this issue.
      Basically, I am using TemplateEngineBlade module which allows me to use Blade templating engine in my templates. I needed to translate strings like '__(<your string>)', but since the module compiles the templates into its own directory (as highlighted in screenshots), the language translator find the function call in 2 files:
      Template where the code is written Modules 'compiled'/cache directory where the templates are in their compiled state Now, I actually have to translate the string in its compiled directory else it  doesn't work at all. But since that filename changes often it is not usable at all.
      I tryed prepending all templates with a file (for example _init.php) and getting all translatable strings there which works but not for the use cases where i need to get same string translated in multiple languages in a single page - I would have to create separate variables.
      Does anyone have any idea which would help me work around my issue?
      Thanks


    • By Mirza
      I have built a system in processwire, which has more than 600K pages.
      A team of 40 people is using the system, DB is from AWS with 16GB Ram.
      But still, select queries are getting locked.
      It would be great if someone suggests how to solve this problem.
      Also note: We have around 48 fields in one template.
      Thanks in advance.