Jump to content

multilingual if statement


Peter Knight
 Share

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?

 

 

 

 

 

 

 

 

 

Link to comment
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?

Link to comment
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?

 

Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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.

Link to comment
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.

 

 

 

 

Link to comment
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; ?>

 

Link to comment
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
Link to comment
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
 Share

×
×
  • Create New...