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

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 Juergen
      Hello @ all,
      I have created an inputfield with a configuration field in the backend where you can set a time format for every language (see screenshot below).

      As you can see both language values (default and German) have the default value(%R), but I have set different values, which were correctly stored in the DB (see screenshot below):

      As you can see the values are '%R' and '%r'.
      I have created the configuration inputfield like this:
      /** @var InputfieldText $f */ $languages = $this->wire('languages'); $f = $this->wire('modules')->get('InputfieldText'); $f->attr('name+id', 'timeformat'); $f->label = $this->_('Timeformat on frontend'); $f->initValue = '%R';//default value $f->attr('value', $this->timeformat ? $this->timeformat : '%R'); $this->message($this->get('timesformat')); if($languages) { $f->useLanguages = true; foreach($languages as $language) { if($language->isDefault()) continue; $f->set("value$language", (string) $this->get("timeformat$language->id")); } } $f->inputType = 'text'; $f->description = $this->_('Please enter the time format that the times should appear on the frontend in strftime format.'); $f->notes = sprintf($this->_('For example shows the time as 08:00, as 08:00 AM. You can find more examples at %s.'), '<a href="https://www.php.net/manual/de/function.strftime.php">https://www.php.net/manual/de/function.strftime.php</a>'); $f->columnWidth = 100; The important part here is:
      if($languages) { $f->useLanguages = true; foreach($languages as $language) { if($language->isDefault()) continue; $f->set("value$language", (string) $this->get("timeformat$language->id")); } } I have borrowed the code from the DateTimeInputfield (https://github.com/processwire/processwire/blob/master/wire/modules/Inputfield/InputfieldDatetime/InputfieldDatetime.module), but the field values will be always populated with the default value ('%R').
      Are I am missing something? Does anyone has experience with multilanguage fields and could help me out?
      Thanks in advance.
    • 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:
      2020/06/29 (June 29, 2020)
      Status:
      Not yet complete. Translations that non-superusers will see is prioritized.
      Name:
      ProcessWire-Norwegian-Language-Pack-nb-NO
      Translated using:
      ProcessWire 3.0.161 dev
      Number of files:
      202 wire files and 97 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 Sten
      Hello
      Till now I hacked something with the twig template but it works no more with new PW versions so I look forward to create a module. I am working on a site in multiple languages : French, English, Italian, German, Spanish, Portuguese, Hebrew, Russian. The new posts are entered in any language with a field for language. Till now, I got twig files to get the translations with constants defined for each part of the pages.
      So I'd like to create a module to include theses files added according to the url /fr/en/...
      Have you some observations to do before I begin about the direction to take ?
      Thank you
    • By ICF Church
      Hi 👋
      Anyone else having this problem?
      Requirements:
      - Repeater (matrix & normal) with mutlilanguage fields (text, textarea…) 
      - Backend language set to something other than default (ie. German) 
      Reproduce:
      - Add a new repeater Item (ajax, I found no way to possible to disable it with matrix)

      (Notice how the default language tab is active instead of the backend language…)
      - Write something into the (default language) field
      - Try to save, if field is required, this will not work. If not required, then when reloading, the content will be inside the backend language field, instead of the default language field who was (presumably) active
      Analysis:
      When  loading  a new repeater element with ajax, the default langue tab is active, but the backend language inputfield is visible (with no visual indication). When writing into the field, it will populate the backend language. When manually clicking on the default language tab (which is already active), the field will switch to the actual default language field (which is [now] empty) (that can now be populated…)
      Also Notice, the labels of the elements to be added are in default language as well instead of the translated label (images instead of Bilder)…
      ProcessWire 3.0.148, Profields 0.0.5…
      Is it my system configuration, or does anyone else have the same issue? This is a screen recording of the problem:
      Issue: https://github.com/processwire/processwire-issues/issues/1179

      Screen Recording 2020-02-25 at 14.18.31.mov
    • By horst
      Change Default Language to be None-English | Walk Trough
      When you start a new (single) language site and the default language shouldn't be English, you can change it this way:
      Go to the modules core section:

       
      Select the Language ones by the filter function:

       
      We have four language related modules here, but for a single language site in none english,
      we only need the base module, named "Languages Support". So go on and install it.

       
      After that, you can leave it, ... 

       
      ... and switch to the newly created Language section under SETUP:

       
      Select the default language

       
      Enter your new language name or its Shortcut and save the page.
      I will use DE for a single language site in german here as example:

       
      Now I go to the ProcessWire online modules directory, down to the subsection for language packs and select
      and download my desired (german) one: 

       

       

       

       
      After downloading a lang pack as ZIP, I go back into my SETUP > LANGUAGES > default language page in admin,
      select the downloaded lang pack ZIP and install it:

       

       

       
       
      After the ZIP is uploaded, the files are extracted and installed, most of my screen is already in
      the new default language. To get all fully switched, we save and leave that page, ...

       
      ... and completely logout from the admin.

       
      Now, of course, we directly login back, ...

      ... and see, that now also the cached parts of the admin have switched to the new default language. 🙂

       
      That was it for a single language site in none english.
       
      If you want to have a multi language site, just add more languages to the SETUP > LANGUAGES section.
      When using a multi language site, I think you also want to use multi language input fields, and maybe different page names for your language page pendents. If so, you need to go into MODULES > CORE > filter LANGUAGE and install what you need or want to use of it, (if not already done).
      Thanks for reading and happy coding, 🙂
       
×
×
  • Create New...