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 Mobiletrooper
      Hey Ryan, hey friends,
      we, Mobile Trooper a digital agency based in Germany, use ProcessWire for an Enterprise-grade Intranet publishing portal which is under heavy development for over 3 years now. Over the years not only the user base grew but also the platform in general. We introduced lots and lots of features thanks to ProcessWire's absurd flexibility. We came along many CMS (or CMFs for that matter) that don't even come close to ProcessWire. Closest we came across was Locomotive (Rails-based) and Pimcore (PHP based).
      So this is not your typical ProcessWire installation in terms of size.
      Currently we count:
      140 Templates (Some have 1 page, some have >6000 pages)
      313 Fields
      ~ 15k Users (For an intranet portal? That's heavy.)
      ~ 195 431 Pages (At least that's the current AUTOINCREMENT)
       
      I think we came to a point where ProcessWire isn't as scalable anymore as it used to be. Our latest research measured over 20 seconds of load time (the time PHP spent scambling the HTML together). That's unacceptable unfortunately. We've implemented common performance strategies like:
      We're running on fat machines (DB server has 32 gigs RAM, Prod Web server has 32gigs as well. Both are running on quadcores (xeons) hosted by Azure.
      We have load balancing in place, but still, a single server needs up to 20 sec to respond to a single request averaging at around about 12 sec.
      In our research we came across pages that sent over 1000 SQL queries with lots of JOINs. This is obviously needed because of PWs architecture (a field a table) but does this slow mySQL down much? For the start page we need to get somewhere around 60-80 pages, each page needs to be queried for ~12 fields to be displayed correctly, is this too much? There are many different fields involved like multiple Page-fields which hold tags, categories etc.
      We installed Profiler Pro but it does not seem to show us the real bottleneck, it just says that everything is kinda slow and sums up to the grand total we mentioned above.
      ProCache does not help us because every user is seeing something different, so we can cache some fragments but they usually measure at around 10ms. We can't spend time optimising if we can't expect an affordable benefit. Therefore we opted against ProCache and used our own module which generates these cache fragments lazily. 
      That speeds up the whole page rendering to ~7 sec, this is acceptable compared to 20sec but still ridiculously long.
      Our page consists of mainly dynamic parts changing every 2-5 minutes. It's different across multiple users based on their location, language and other preferences.
      We also have about 120 people working on the processwire backend the whole day concurrently.
       
      What do you guys think?
      Here are my questions, hopefully we can collect these in a wiki or something because I'm sure more and more people will hit that break sooner than they hoped they would:
       
      - Should we opt for optimising the database? Since >2k per request is a lot even for a mysql server, webserver cpu is basically idling at that time.
      - Do you think at this point it makes sense to use ProcessWire as a simple REST API?
      - In your experience, what fieldtypes are expensive? Page? RepeaterMatrix?
      - Ryan, what do you consider as the primary bottleneck of processwire?
      - Is the amount of fields too much? Would it be better if we would try to reuse fields as much as possible?
      - Is there an option to hook onto ProcessWires SQL builder? So we can write custom SQL for some selectors?
       
      Thanks and lots of wishes,
      Pascal from Mobile Trooper
       
       
    • By Sten
      Hello,
       
      This is not directly about language module but I think I can get information from you.
      Can I add a vendor module to have all languages written automatically into name?
      I used this vendor module which is good with any language (hebrew, arabic...). How could I add it, so admin interface can use it ?
      Thank you
    • By Sergio
      All of a sudden, with nothing changed on the database or server, a website was getting error when doing a search:
      Error: Exception: SQLSTATE[HY000]: General error: 23 Out of resources when opening file './your-database-name/pages_parents.MYD' (Errcode: 24 - Too many open files) (in /home/forge/example.com/public/wire/core/PageFinder.php line 413) #0 /home/forge/example.com/public/wire/core/Wire.php(386): ProcessWire\PageFinder->___find(Object(ProcessWire\Selectors), Array) #1 /home/forge/example.com/public/wire/core/WireHooks.php(723): ProcessWire\Wire->_callMethod('___find', Array) #2 /home/forge/example.com/public/wire/core/Wire.php(442): ProcessWire\WireHooks->runHooks(Object(ProcessWire\PageFinder), 'find', Array) #3 /home/forge/example.com/public/wire/core/PagesLoader.php(248): ProcessWire\Wire->__call('find', Array) #4 /home/forge/example.com/public/wire/core/Pages.php(232): ProcessWire\PagesLoader->find('title~=EAP, lim...', Array) #5 /home/forge/example.com/public/wire/core/Wire.php(383): ProcessWire\Pages->___find('title~=EAP, lim...') #6 /home/forge/example.com/public/wire This error message was shown because: you are logged in as a Superuser. Error has been logged.  
      I tried several things, listed in this thread: https://serverfault.com/questions/791729/ubuntu-16-04-server-mysql-open-file-limit-wont-go-higher-than-65536
      But for some reason, MySQL was not getting its limit increased, but in the end, the one that did the trick was this:
      This worked for me on Ubuntu Xenial 16.04:
      Create the dir /etc/systemd/system/mysql.service.d
      Put in /etc/systemd/system/mysql.service.d/override.conf:
      [Service] LimitNOFILE=1024000 Now execute
      systemctl daemon-reload systemctl restart mysql.service Yes indeed, LimitNOFILE=infinity actually seems to set it to 65536.
      You can validate the above after starting MySQL by doing:
      cat /proc/$(pgrep mysql)/limits | grep files
    • By bot19
      I've never encountered this issue before. My local installing is using AMPPS 3.7, setup about 3 months ago.
      Everything was working fine last night, and I think possibly this morning, before it complete stopped with these errors. Please see below.
      I have not changed any config setttings, access settings.
      The only 2 other things I did yesterday was log into phpmyadmin and export the DB, then I think an Xcode update was installed yesterday.
      Can anyone help?
      I know people have run into these issues when they migrate, but I haven't migrated, setup, changed anything.
      Some people have said to just disable to do something like:
      $cfg['Servers'][$i]['AllowNoPassword'] = true; In phpMyAdmin's config.inc.php file. I tried but that didn't work.
      I even tried changing:
      $config->dbHost = 'localhost'; to 127.0.0.1, in site/config.php but no good.


    • 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?