Nukro

Weird behaviors with Processwire Site Translation Files / Code Internalization

Recommended Posts

Hi Guys

I have a problem with the Code Internationalization (Site Translation Files) in Processwire. I am using the method Ryan is describing in this topic and also the normal way of translating files. The weird behaviors are that translations are just gone after a change in the main translation file called _strings (uses Ryan method). Or some other weird behavior was, that the english translation of the _strings file were somehow ported inside the german _strings file. I really don't know what could cause this. Have ever witnessed something like that? 

I have made some thoughts about this whole Code Internalization thing in ProcessWire and I am thinking to go another way (don't get me wrong, it's perfect for small to mid projects but in my opinion it is just a overkill for big projects), because at the beginning the strings we needed to translate were splitted up in many files which produced many site translation files to translate per language. It was just to much and confusing for the customer (and also for me), so that we needed another approach. So I tried Ryans method with one central translation files. The problem with this one is that it also gets confusing since it is a very big amount of strings to translate and also the problems described above. So for know I will try to make approach to store the strings in Pages. All translations of a string would be stored side by side. I know that this is probably more complex behind the scenes but it is far easier for the customer (especially with ListerPro). And I also like the idea of having the translations inside the DB instead in form of Files.

What are your thoughts about it? Have ever done something like that?

Greetings

Orkun

Share this post


Link to post
Share on other sites

I tried to make a function, which I can fetch translations from a page by it's name field.

Features of the function:

  • Fetches translations from page title (multilanguage).
  • Outputs an edit link to the Translation Page (doesn't output it when 2nd argument is false)
  • When the translation page you are referencing is not found, it outputs an error and also the absolute path to the php file were the function was executed by using debug_backtrace function of PHP.

Benefits of this approach:

  • You have full access to the ProcessWire API in terms of manipulating the translation data
  • Translations of single Strings in different languages are side by side (tabs)
  • Searching of single Translations is easy with Listers (I know search of site translation files is supported in PW3 but not in PW2, and I am using PW 2.7.3)
  • Usability is far better since the users doesn't have to fiddle around in the system languages itself.

Possible disadvantages:

  • Little overhead when using the function, since it always loads a page behind the scenes when the function is used. (I had used 8-13 function calls inside a template file and doesn't had noticed any performance issues (bless processwire)).
function _gt($key, $edit = TRUE){
    $link = "";
    $tracemessage = "";
    $string = "";

    $translationPage = wire('pages')->get("template=translation, name=$key");
    if($translationPage->id){
      if($edit && wire('user')->isLoggedin()){
        if(wire('user')->isSuperuser()){
          $editlink = " <a style='color:green;' target='_blank' href='".$translationPage->editUrl()."'>[Edit]</a>";
        } 
      } 
      $string = $translationPage->title.$editlink;
    }else{
      if(wire('user')->isLoggedin()){
        $trace = debug_backtrace();
        $tracemessage = "(".$trace[0]["file"].")";
      }
      $string = "<span style='color:white;background-color:red;padding:5px;'>[Not Found: '".$key."'] $tracemessage</span>";
    }

    return $string;
}

 

Example:

echo _gt("akkreditierung");
echo _gt("fachgebiete-doctor");

58d035d26af35_Bildschirmfoto2017-03-20um20_58_23.png.f28adfef7825306606551b9b14453d20.png

 

And when it fails to find the translation page:

58d036281a404_Bildschirmfoto2017-03-20um21_00_26.thumb.png.3b06bcd45a33104b73a1443d012e45a9.png

Edited by Nukro
Function Rewrite, better explanation of use

Share this post


Link to post
Share on other sites

I am quite happy with translation options included in PW.

Translatable strings like

$out = $this->_("Live long and prosper");  // syntax within a class (modules)
$out = __("Live long and prosper!"); // syntax outside of a class (templates)

are useful for modules and (rarely) in the template system.

To make them accesible globally I define a page property in my _init.php file which is prepended to each template.

/**
 * define globally used translatable strings here
 */
$page->__read_more = __('Read more');
$page->__submit = __('Submit');

 

Furthermore I use some language related functions (language specific date and time formats). These functions are placed in _func.inc which is included by the _init.php

// Include shared functions
include_once("_func.inc");

Everything else is content and should be stored in multilanguage fields. (text, textarea, optionselect)

  • Like 3

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 K4mil
      Hello
      I've recently run into a trouble with translatable strings in my template files. Shortly speaking translatable strings show up properly in language panel, I can create and save translations, everything looks just fine in backend. The problem is that the only outputed value is the hardcoded one. It's definitely not a problem with template code itself because this happened even on simple test template with a  translatable string alone. 
      There are no errors, I don't see anything special in logs. I just recently moved site from my local server to my ftp. There were some issues with paths in language files, but I have fixed it. It's the newest dev version of PW. I really don't have idea where to look, I'm fighting with this second day, and slowly loosing my nerves.
    • By prestoav
      Hi all,
      A very odd issue this one!
      I'm working on a site with 5 languages - English (default), French, German, Spanish and Polish. I have a set of pages set up as a country list (templae='country') which contain various country info like two-letter country code etc and where the page title is the Country name. This is so that I can use the country list in various parts of the site.
      When the site is showing in English, Spanish and Polish I can display the country list without an issue. However, when the page is displaying in French or German the country pages seem not to exist. So, this code...
       
      $countries = $pages->find("template=country"); foreach ($countries as $country) { if ($country) { echo $country->title . "<br />"; } } ...displays the country list just fine in English, Spanish and Polish but displays nothing in French and German.
      Just for extra info, the Title field is set up as PageTitleLanguage and all languages are setup the same.
      Any ideas?
      TIA.
    • By Erbenos
      Hello,
      recently i came across this issue.
      Basically, I am using TemplateEngineBlade module which allows me to use Blade templating engine in my templates. I needed to translate strings like '__(<your string>)', but since the module compiles the templates into its own directory (as highlighted in screenshots), the language translator find the function call in 2 files:
      Template where the code is written Modules 'compiled'/cache directory where the templates are in their compiled state Now, I actually have to translate the string in its compiled directory else it  doesn't work at all. But since that filename changes often it is not usable at all.
      I tryed prepending all templates with a file (for example _init.php) and getting all translatable strings there which works but not for the use cases where i need to get same string translated in multiple languages in a single page - I would have to create separate variables.
      Does anyone have any idea which would help me work around my issue?
      Thanks


    • By Peter Knight
      I have a dropdown menu which displays a list of languages installed on my site. 
      Id like to add some country flags to the menu list items and the simplest way would be to add an image field to the languages template which is installed in the settings branch. 
      However it seems like this system template won't accept new fields but isn't locked. Is there a way to extend this template to accept new fields?
      P
       
    • By bmacnaughton
      This is probably really simple but I am unable to get the two letter code for the default language.
      $languages->default gives me a numeric code (the page ID for that language).
      $user->language give me the numeric code again
      $user->languages->name gives me "default" if it is the default language, else the two-letter code for the language.
      But I don't see the two letter code for the language in $config (I might be missing it) nor anywhere else.
       
      I've hardcoded around this now, so the question is largely academic, but I'd prefer not to hardcode it.
       
      Being able to get the two character code, and not 'default' for the default language would be equally helpful.