Jump to content

How to do this? Global vars?


OllieMackJames
 Share

Recommended Posts

I have a website design that will be used in at least three languages AND on three different domains.

In the templates I have some stuff hard coded that I want to change in one place.

Please tell me the best way to go about this, somewhere I read that global vars is not a good thing with processwire.

I tried a variation with the translator module from pw, but find that very cumbersome, I am looking for a solution to just have one language file with the vars that will then be used all over the place, within or outside of fucntions.

I now have made one language-nl.php file which goes like this:

<?php
global $more_articles;
$more_articles = "Meer artikelen hier onder:";
global $add_to_cart;
$add_to_cart = "In Winkelwagen";
global $must_see_cap;
$must_see_cap = "AANBEVOLEN";
global $must_see;
$must_see = "Aanbevolen";
global $related_articles;
$related_articles = "Gerelateerde Artikelen";
global $most_popular;
$most_popular = "Meest Populair";
global $add_to_cart_cap;
$add_to_cart_cap = "IN WINKELWAGEN";
?>

I needed to go with global vars because in some templates I use code like this:

function getPromoted($page, $related = false) {
		global $related_articles;
		$promotedPages = $page->promoted_articles;
		
		$items = "";

		if (!count($promotedPages)) return;
		if (!$related) {
			$items .= "<div class='promoted'>";
		}
		else {
			$items .= "<header><h2>$related_articles</h2></header>";
		}

		$items .= "<div class='article-list'>";

		foreach ($promotedPages->not(wire('page')->id) as $p) {
			$category = $p->rootParent->id == 1015 ? "back-pain" : ($p->rootParent->id == 1016 ? "bulging-disc" : ($p->rootParent->id == 1017) ? "sciatica" : null );
			$img = "";
			$isVideo = $p->video_embeding ? 'isVideo' : '';

			if (count($p->image)) {
				$img = $p->image;
			}
			else if ($p->video_images->first()) {
				$img = $p->video_images->first();
			}
			list($width, $height) = @getimagesize($img->getThumb('listingthumb','path'));

			$items .= "
				<article class='category-" . $category . " " . $isVideo . "' itemscope itemscope itemtype='http://schema.org/ItemList'>
					<div class='inner' itemprop='itemListElement'>
						<a itemprop='url' href='" .$p->url. "'>
							<div class='picture'>
								<div class='label'>" . $p->rootParent->title . "</div>
								<img itemprop='image' src='".$img->getThumb('listingthumb')."' width='".$width."' height='".$height."' alt='".$p->title."' />
							</div>
							<h5 itemprop='name' class='title'>".$p->title."</h5>
						</a>
					</div>
				</article>
			";
		}

		$items .= "</div>";
		if (!$related) $items .= "</div>";

		return $items;

	}

Any ideas how to do this simpler and smarter?

Thanks!

Link to comment
Share on other sites

I believe you should use the built-in string translator, but here is a simpler solution. It's probably not the most optimized one but it should work - put it in _func.php:

<?php

function getTranslations() {
    return array(
        "Read more" => array(
            "nl" => "Lees meer",
            "de" => "Weiter lesen"
        ),
        "Must see" => array(
            "nl" => "Must see",
            "de" => "Muss sehen"
        )
    );
}

function myTranslate($key, $locale = '') {
    if($locale == '') {
        $locale = $user->language->name;
    }
    $translations = getTranslations();
    return (array_key_exists($locale, $translations[$key])) ? $translations[$key][$locale] : $key;
}

// usage
echo myTranslate("Read more"); // current user's locale
echo "<br/>";
echo myTranslate("Read more", "de"); // DE
echo "<br/>";
echo myTranslate("Read more", "es"); // non-existing locale: original string ("Read more")
Link to comment
Share on other sites

tpr, thanks for chiming in

I have no _func.php file and I don't want extra logic checking visitor locale.

For each language I just have a separate domain, all optimezed for seo for that local g'gle

And next to that I expect this code to give me trouble within functions again, but thanks for chiming in!

diogo, can you please elaborate, this sounds like what I want, but am not sure how to do that.

Is this what I should do?:

1 - make template site-wide-translations.php

2 - have fields for that template with the label of the field the default text in english

3 - in the page based on that template and with all the fields I input the correct words for this language version?

The above I can figure out, now the mystery for me is:

- how to use the fields in other templates, how do I call on them?

- and how do I do that within extra functions within those templates?

Thanks!

Link to comment
Share on other sites

You simply create a new template with a multilanguage text field for each expression you want to translate. the name of the fields and labels can be in english, but not necessarily the words that you will output, they would be just informative. Then you fill the fields with all the languages.

To output them in other templates, you would do this:

// top of the file
$translations = $pages->get('/translations/');

// and when needed
echo $translations->more_articles;

// inside functions
echo wire('pages')->get('/translations/')->more_articles;
  • Like 4
Link to comment
Share on other sites

You simply create a new template with a multilanguage text field for each expression you want to translate. the name of the fields and labels can be in english, but not necessarily the words that you will output, they would be just informative. Then you fill the fields with all the languages.

To output them in other templates, you would do this:

// top of the file
$translations = $pages->get('/translations/');

// and when needed
echo $translations->more_articles;

// inside functions
echo wire('pages')->get('/translations/')->more_articles;

Thanks diogo! This is what I was looking for, very happy now!

  • Like 1
Link to comment
Share on other sites

  • 3 years later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...