Jump to content

Recommended Posts

I was trying to fix an error earlier which was preventing a series of if statements from working.

Basically I have a button called "Product Drawing" which needs localising depending on the current language session.

Eventually got it working - I had a bracket in the wrong place.

 

Curious as to why both these work though.

On the sample below. each echo is surrounded by curly braces.

<?php 
if ($page->files->count())
foreach($page->files->findTag('drawing') as $file)	
{
if($user->language->name == 'default') {echo "<a href='$file->url' class='uk-button uk-button-primary'>Product drawing</a>";}	
if($user->language->name == 'french') {echo "<a href='$file->url' class='uk-button uk-button-primary'>Le plan du produit</a>";}	
if($user->language->name == 'german') {echo "<a href='$file->url' class='uk-button uk-button-primary'>Produktzeichnung</a>";}		
if($user->language->name == 'spanish') {echo "<a href='$file->url' class='uk-button uk-button-primary'>El dibujo del producto</a>";}	
if($user->language->name == 'italian') {echo "<a href='$file->url' class='uk-button uk-button-primary'>Il disegno del prodotto</a>";}	
} 
?>

 

On the sample below, there are no curly braces around the echo

<?php		
if ($page->files->count())
foreach($page->files->findTag('breakingreport') as $file)	

{
if($user->language->name == 'default') echo "<a href='$file->url' class='uk-button uk-button-primary'>Breaking strain report</a>";	
if($user->language->name == 'french') echo "<a href='$file->url' class='uk-button uk-button-primary'>Le test de rupture</a>";	
if($user->language->name == 'german') echo "<a href='$file->url' class='uk-button uk-button-primary'>Bruchfestigkeit bericht</a>";		
if($user->language->name == 'spanish') echo "<a href='$file->url' class='uk-button uk-button-primary'>Informe resistencia</a>";	
if($user->language->name == 'italian') echo "<a href='$file->url' class='uk-button uk-button-primary'>La rottura rapporto ceppo</a>";	
} 
?>

 

Should the second piece work?

 

 

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

Yes. PHP's "if" affects the immediately following statement or block, so both versions are fine.

Share this post


Link to post
Share on other sites
5 hours ago, Peter Knight said:

Basically I have a button called "Product Drawing" which needs localising de

I'm curious, why didn't you use PW's translation capabilities, in this case, to avoid the code repetition?

Share this post


Link to post
Share on other sites
1 minute ago, Sérgio Jardim said:

I'm curious, why didn't you use PW's translation capabilities, in this case, to avoid the code repetition?

You mean create a field somewhere called "download button" and translate it with the field's language tabs?

 

Share this post


Link to post
Share on other sites

Yes if you expect the user to translate it by him/herself.

But if it's your job, just wrap the string in code and translate it in the language settings page, loading the PHP file there.

Although, I've found the use of a single file to handle all strings, like a _translations.php much easier. I just include it within my _init.php. I have one here with 225 phrases and counting. :)

  • Like 1

Share this post


Link to post
Share on other sites
translations.php
_x('Поиск', 'General');


function _t($text, $context = 'General', $textdomain = '/site/templates/helpers/translations.php') {
  return _x($text, $context, $textdomain);
}

echo _t('Поиск');
  • Like 1

Share this post


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

I'm not sure how to do that. can you elaborate with a simple example?

There's this thread:

 

  • Like 1

Share this post


Link to post
Share on other sites

@Peter Knight Here is exactly what you need to do:

Create a new file under templates: _translations.php. Inside add all your translatable words or phrases like this:

$page->__product_drawing = __('Product Drawing');
$page->__buy_now = __('Buy Now');
$page->__read_more = __('Read more');

Then on your _init.php add this:

include_once("./_translations.php");

Then on the templates you want to display for example the "Product Drawing" add this:

$page->__product_drawing

Now all you have to do is to go on your Languages under admin and then find the file _translations.php and start translating all the strings you have there.

  • Like 1

Share this post


Link to post
Share on other sites

@PWaddict I'm not really sure why you would bind your translations to the page object. What if you want to use those in a context, where there's no $page variable present? Wouldn't it be better to just create a new WireData object (like $static_translations) and return that. 

  • Like 2

Share this post


Link to post
Share on other sites
54 minutes ago, LostKobrakai said:

@PWaddict I'm not really sure why you would bind your translations to the page object. What if you want to use those in a context, where there's no $page variable present? Wouldn't it be better to just create a new WireData object (like $static_translations) and return that. 

I'm using it on PW & Padloper templates and it's working fine. Although I haven't tested yet the email templates from Padloper. If you want post all the steps required in case I need to do your method.

Share this post


Link to post
Share on other sites
On 08/07/2017 at 7:27 AM, PWaddict said:

@Peter Knight Here is exactly what you need to do:

Create a new file under templates: _translations.php. Inside add all your translatable words or phrases like this:


$page->__product_drawing = __('Product Drawing');
$page->__buy_now = __('Buy Now');
$page->__read_more = __('Read more');

 

Ok so I create a php file called _translations.php. I upload it to site/templates/ directory.

Do I then need to add it as a template under Admin > Setup > Templates  > Add new template?

On 08/07/2017 at 7:27 AM, PWaddict said:

Then on your _init.php add this:


include_once("./_translations.php");

 

I don't already have that file. if I create a file called _init.php does it live under /site/ or /site/templates?

Quote

go on your Languages under admin and then find the file _translations.php and start translating all the strings you have there.

My Languages page under Admin > Setup  just has my 5 languages listed. 

If I click into the French one, the only thing I can see is a field called Core Translation Files

I know I'm probably missing an earlier step.

 

 

 

 

Share this post


Link to post
Share on other sites

OK I think I see my missign step.


When I click into a language under Admin > Setup > Languages

I need to look at a field group called Site Translation Files

I need to select the button called Find Files to Translate which brings up a list of files in /site/ and then select my _translations.php file

The only issue I have is adding this to my template outputs nothing

<?php echo $page->__product_drawing ?>

Also tried 

<?php echo $page->__product_drawing; ?>

 

Share this post


Link to post
Share on other sites
/**
 * Prepend template file
 *
 * PHP file in /site/templates/ that will be loaded before each page's template file.
 * Example: _init.php
 *
 * @var string
 *
 */
$config->prependTemplateFile = '_init.php';

 

Share this post


Link to post
Share on other sites
2 hours ago, Peter Knight said:

The only issue I have is adding this to my template outputs nothing

1. Put the _init.php under site/templates directory.

2. Add the following code on the site/config.php file and you're ready:

/**
 * Prepend template file
 *
 * PHP file in /site/templates/ that will be loaded before each page's template file.
 * Example: _init.php
 *
 * @var string
 *
 */
$config->prependTemplateFile = '_init.php';

 

  • Like 1

Share this post


Link to post
Share on other sites

Thanks @PWaddict

All working now :)

Thanks everyone for introducing me to something new.

I can see how this will be useful as the site grows. Even now it easily replaces several lines of PHP if statements .

 

  • Like 2

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By snobjorn
      Here's my Norwegian language pack for ProcessWire. I've been adding translations over some time, to suit my own projects users needs.
      Last updated:
      2019/06/19 (June 19, 2019)
      Status:
      Not yet complete. Translations that non-superusers will see is prioritized.
      Name:
      ProcessWire-Norwegian-Language-Pack-nb-NO
      Translated using:
      ProcessWire 3.0.133 dev
      Number of files:
      189 wire files and 95 site files – ranging from just started to completely translated. The site files includes translation some free and some pro modules, see the complete list in the README.md at GitHub.
      Available at GitHub:
      https://github.com/snobjorn/processwire-norwegian-language-pack-nb-no
    • By louisstephens
      Hopefully this is the right place, if not, please move it to the proper sub. I installed the multi language module on my development (sandbox) set up not too long ago to test out a few things. From the start, I ran into some issues with dependencies not being met/installed, so I thought that I would simply just uninstall what had been. Unfortunately, that took me down a road of using some code in my home template to remove the modules (cant find it at the moment). After some struggling, I finally got it uninstalled, but it left behind 3 fields,
      language language_files language_files_site I thought that I could simply remove them from the languages template and then delete them, but I get the following error:

      I was wondering, is there a way to remove these using the api? I couldn't add any new users to the setup until I made "language" not required, but I was hoping to just remove all of the fields.
    • By bartelsmedia
      "Deepl" is an unbelievably good (paid) text translation service, that mops the floor with Google Translate.
      They have an API and it would be awesome, if there would be a module, that would integrate it for easy content translation.
      It could be a right-click context menu on the language tab:

      It should ask from which of available other language it shall take translate from.
      What do you think?
    • By Gadgetto
      Hello,
      I'm very new to ProcessWire but already fell in love with this CMS/CMF! I just finished my first small project and as I saw a lot of questions and different answers in this forum on how to set up a nice language switcher for your website, I decided to write my first tutorial.
      ----------
      Please note:
      I rewrote this tutorial since I was made aware and learned that flags should not be used for language selectors!
      There are some threads here in the forum (and from external sources) where this question is discussed:
      https://processwire.com/talk/topic/13196-adding-image-field-to-language/
      http://daily.unitedlanguagegroup.com/stories/editorials/inside-design-language-selector-no-flags
      https://processwire.com/talk/topic/16524-extending-languages-template/
      http://www.flagsarenotlanguages.com/blog/why-flags-do-not-represent-language/
      https://processwire.com/talk/topic/14241-language-names-and-utf8-page-names/
      Thanks,
      @ottogal
      @bernhard
      @jmartsch
      @kongondo
      an all others for your hints!
      ----------
       
      TUTORIAL - Set up a nice language switcher for your website - here we go:
       

      This will be the desired result!
       
      Step 1)
      Setup at least 2 languages in your PW install. In my case it's German (default language) + English:
       

       
      Step 2)
      Add a custom field
      Type = Text
      Name = languagecode
      This will hold the ISO 639-1 two-letter language code for the respective language. The field is needed to provide a simple method for outputting the language code in your templates. Without this field, you will need to programmatically construct your two-letter language code output via PHP (at least for the default language, as ProcessWire doesn't allow to rename the default language and it will always be called default).
      Here is an overview for ISO 639-1 two-letter language codes:
      https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
       

       
      Step 3)
      Add this field to the system template: language.
      To achieve this, go to Setup / Templates and activate the filter Show system templates:
       

       
      Now you can add the previously created field languagecode to the language template.
       

       
      Step 4)
      Edit your languages and fill in the appropriate values.
      a) default (German)
      Name = default (this can't be changed and is read only)
      Title = Deutsch (in both language tabs! - this is important as your visitor should always see his language item ... in his language)
      languagecode = de
       

       
      b) english (English)
      Name = english
      Title = English (in both language tabs! - this is important as your visitor should always see his language item ... in his language)
      languagecode = en
       

       
      Step 5)
      Now we are ready to write our template output!

      As we already have the appropriate two-letter ISO language code (languagecode field), we can use this in our html lang property:
       
      <html lang="<?php echo $user->language->languagecode; ?>">  
      Also the rel alternate output in the html head is simple. Put the following code within your <head></head> area:
       
      <?php // Handle output of 'hreflang' link tags for multi-language (SEO!) foreach ($languages as $language) { if (!$page->viewable($language)) { continue; } // Get the http URL for this page in the given language $url = $page->localHttpUrl($language); // Get the language code using custom languagecode field $languagecode = $language->languagecode; echo PHP_EOL.'<link rel="alternate" hreflang="'.$languagecode.'" href="'.$url.'">'; } ?>  
      In my sample I've used Boostrap 4 and the code below shows a complete navbar with our language switcher (BTW the language switcher will always be visible, even when the bootstrap navbar is collapsed):
       
      <nav id="mainnav" class="navbar navbar-expand-lg navbar-light px-4 px-md-5 sticky-top"> <a class="navbar-brand" href="<?php echo $config->urls->root; ?>"> <img src="<?php echo $config->urls->templates; ?>images/logo-rund-80x80.png" alt=""> Your Site Title </a> <ul class="navbar-nav ml-auto mr-3 mr-lg-0 order-lg-last d-none d-xs-custom-flex language-switcher" aria-label="<?php echo __('Sprache wechseln') ?>"> <?php echo '<li class="nav-item dropdown">'; // Construct the language prompt in the current user language $prompt = $user->language->title.' ('.strtoupper($user->language->languagecode).')'; // Current language = dropdown-toggle echo '<a class="nav-link dropdown-toggle" href="#languages" id="language-select" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">'; echo '<span class="world-icon"></span><span class="sr-only">'._x('(aktuelle Sprache)', 'navigation').': </span> '.$prompt; echo '</a>'; echo '<div id="languages" class="dropdown-menu dropdown-menu-right" aria-labelledby="language-select">'; foreach ($languages as $language) { // Get the http URL for current page in the given language $url = $page->localHttpUrl($language); // Construct the language prompt in the given language $prompt = $language->title.' ('.strtoupper($language->languagecode).')'; // Next language item (except current language) if ($user->language->id != $language->id) { if (!$page->viewable($language)) { echo '<span class="dropdown-item disabled">'.$prompt.'</span>'; } else { echo '<a class="dropdown-item" href="'.$url.'">'.$prompt.'</a>'; } } } echo '</div>'; echo '</li>'; ?> </ul> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarMainMenu" aria-controls="navbarMainMenu" aria-expanded="false" aria-label="<?php echo __('Menü einblenden / ausblenden') ?>"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse my-3 my-lg-0" id="navbarMainMenu"> <ul class="navbar-nav mr-auto"> <?php // Top navigation consists of homepage and its visible children foreach ($homepage->and($homepage->children("template=main-page|news|contact-points")) as $item) { if ($item->id == $page->rootParent->id) { echo '<li class="nav-item active">'; echo '<a class="nav-link" href="'.$item->url.'">'.$item->title.'<span class="sr-only"> '._x('(aktuelle Seite)', 'navigation').'</span></a>'; echo '</li>'; } else { echo '<li class="nav-item">'; echo '<a class="nav-link" href="'.$item->url.'">'.$item->title.'</a>'; echo '</li>'; } } ?> </ul> </div> </nav>  

      That's it! I hope you will like my tutorial and if there are any questions, critics or improvements please let me know!
      Thanks,
      Martin
       
    • By tron1000
      Hello! After I upgraded PW from 2.5.? to 3.0.116, my language tabs in the backend behave strange. I have 3 languages: german, french and english (default). Since I upgraded, all multilingual fields show the 3 tabs (DE, FR, EN) plus an additional textfield below for EN (see screenshots). The german tab is ok, but the upper tabs FR and EN both show the content in french, while the additional tab below shows the english content. When I click on the folder icon to the right to change the view, all looks ok. Also the frontend is ok.
      I uninstalled and reinstalled the module «LanguageTabs», but that didn't help …
      Any ideas?
      Thanks a lot, Andrej


×
×
  • Create New...