Jump to content

How to include _strings.php on entire site using Wire Render Pattern?


Clarity
 Share

Recommended Posts

Hello everyone!

I have _strings.php file which I need to use on entire site. It is included in _init.php file like that: 

include_once("{$config->paths->templates}_strings.php"); 
 
However, our site uses partially uses Wire Render Pattern (https://github.com/joyofpw/wire-render-pattern), which is why _strings.php isn't getting included to some files automatically. Can you please tell us what to do in this case?
Link to comment
Share on other sites

  • Clarity changed the title to How to include _strings.php on entire site using Wire Render Pattern?

What does it contain, whats in the _strings.php? What does it provide? (functions, vars, ...?)

Maybe another method than include_once() could be more straight forward and also works with wire-render-pattern.

  • Like 1
Link to comment
Share on other sites

On 9/2/2021 at 4:46 PM, Clarity said:

_strings.php contains variables which are translations from one language to another (e.g. $var1 = __('Text 1')). I thought it is a standard ProcessWire file.

To be honest, I don't know if this is a standard processwire file.

But with language files one can use it as follows:

Instead of adding each string to an own variable, you can add a second parameter to all the language strings you use around in different template files or module files or where ever you need them:
__('Text 1', '/path/to/your/_strings.php');
__('Text 2', '/path/to/your/_strings.php');
...
And instead of adding the '/path/to/your/_strings.php' as text all the time, I use a functions call for that. This way you do not have to include the file and can use it everywhere handled through the PW translations system, because you tell it that the current string is the same as a string in the centralized _strings.php

In /site/init.php I define this function, that simply returns the path to my centralized translation file:

    /**
     * TRANSLATABLE STRINGS
     * Include translatable strings
    */
    if(!function_exists('ProcessWire\mystrings')) {
        function mystrings() {
            return '/site/_strings.php'; // absolute URL or absolute filepath to the central translation file
        }
    }

My _strings.php would look like this:

<?php namespace ProcessWire;
/**
 * TRANSLATABLE STRINGS
 * Define globally available translatable strings
 *
 * USAGE
 *
 * __('Lesen Sie mehr ...', mystrings());
 *
 * The function mystrings() returns the textdomain of this file.
 *
**/

...
__('Datenschutzerklärung');
__('Die Maße des Werks sind:');
__('Dieses Bild drucken!');
__('drucken');
...
__('Text 1');
__('Text 2');
...
__('Zurück zur Übersicht');

 

To sum up, this way I don't need to include a _strings.php and it also works every where.

 

  • Like 4
  • Thanks 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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...