Jump to content

Can you print a static translation in multiple languages?


sodesign
 Share

Recommended Posts

I'm writing a custom translation exported for a client and I'm having trouble outputting static translations in languages other than the default.

Here's what I'm trying to do, but it's just outputting English both times. I must be missing something, but can't see anything in the docs or forum about how to acheive this. 

<?php 

function echoSnippet($string, $lang) {
  // Echo the string in english
  wire('user')->setLanguage(wire('languages')->get('name=en'));
  echo __($string);
  
  // Echo the string in german
  wire('user')->setLanguage($lang);
  echo __($string);
}

$lang = $languages->get("name=de");
echoSnippet("Page not found", $lang);

 Does anyone have any pointers?

Thanks!

Link to comment
Share on other sites

  • 4 months later...

Hello,

I suppose this is due to text domain.

You are asking for a translation that has been defined in another file, so to get it you need to specify the text domain of this file.

echo __($string, '/site/templates/file_containing_translation.php');

If you want to automatically export translations, I think you should put all translations in a single file, let say it's named _commonTranslations.php:

__('translation 1');
__('translation 2');
// And so on...

Then when you insert this translations in the template that needs it, you need to specify text domain (you can put it in a constant or create a global function that inserts it automatically):

__('translation 1', '/site/templates/_commonTranslations.php');

Note that in PW, you translate this sentences ONLY in _commonTranslations.php.

 

Finally your function looks like that:

function echoSnippet($string, $lang) {
  // Echo the string in english
  wire('user')->setLanguage(wire('languages')->get('name=en'));
  echo __($string, '/site/templates/_commonTranslations.php');
  
  // Echo the string in german
  wire('user')->setLanguage($lang);
  echo __($string, '/site/templates/_commonTranslations.php');
}

 

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

  • Recently Browsing   0 members

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