pug-php / pug

Pug template engine for PHP
https://www.phug-lang.com
MIT License
387 stars 42 forks source link

localisation of strings #131

Closed sandrodz closed 6 years ago

sandrodz commented 7 years ago

I need to run all strings in plain language in a php localisation function.

e.g. __( 'some string', 'mytheme' ) or _e( 'some string', 'mytheme' ) (shortcuts to php gettext)

What is the best way to achieve this?

I could build a data array and pre populate it with strings and pass it to pug:

$data = [
  'string1' => __( 'some string', 'mytheme' )
];

and than interpolate string1 inside pug.

But surely there is a better way :)

kylekatarnls commented 7 years ago

First gettext can parse pug files (I use python language, maybe now there is a pug option right in the settings), so you can use right in templates:

p=_("Hello world")

I would recommend this approach because it makes templates easiest to work with and avoid passing a lot of data from the controller.

If you have some common and dynamic texts, use the share method:

$pug->share(array(
  'language' => 'en',
  'title' => 'My website'
));
sandrodz commented 7 years ago

works great :) I use both methods yes.

btw, it needs textdomain otherwise it doesn't work in php. e.g. p=__("Hello world","domain")

Is it possible to add domain during compilation? just to have pug files cleaner.

kylekatarnls commented 7 years ago

Just add this on the top of the template or in a layout/include:

- textdomain("domain")
header
  p=_("Hello world")
  | Pug template content...
sandrodz commented 7 years ago

Thanks!

sandrodz commented 7 years ago

@kylekatarnls last question I promise ^_^ do you know any more elegant way of writing these?

#{__()} seems terrible.

sandrodz commented 7 years ago
screen shot 2017-05-05 at 8 28 56 pm
kylekatarnls commented 7 years ago

Not realy better. You can do this:

| 120
=_(...)
=" "
=_(...)
sandrodz commented 6 years ago

@kylekatarnls

I get this error: Undefined variable: textdomain on line 1, offset 1

screen shot 2018-01-01 at 8 44 07 pm
kylekatarnls commented 6 years ago

Hi, I suspect gettext to not be installed and enabled, can you check it with phpinfo()?

sandrodz commented 6 years ago

I have the module:

screen shot 2018-01-01 at 8 56 23 pm

sandrodz commented 6 years ago

I'm using: 'expressionLanguage' => 'js'

kylekatarnls commented 6 years ago

Authors are just credits of installed modules. They can be installed without being enabled.

The needed setting is:

phpinfo-gettext

Also be sure, this is the same PHP program (run - phpinfo() directly from the pug template to be sure), you can have Apache and CLI with different settings, and potentially different versions of PHP installed.

sandrodz commented 6 years ago

Yes you were right. Updated my docker containers with gettext module: https://github.com/wearede/DevCeption/commit/6f72470ca022c4cb3dc8e540842a350555f7c6f4

kylekatarnls commented 6 years ago

Great!

sandrodz commented 6 years ago

Hi, I'm still having issues with this.

It only works if I add text domain inside the __ calls e.g.

screen shot 2018-01-12 at 9 25 08 pm

Without text domain strings are not translated.

I have:

- textdomain(txtdomain)¬ at the beginning of the base pug file.

kylekatarnls commented 6 years ago

Hi,

The gettext function take exactly 1 argument, no more. And this come with - textdomain() function separated.

Can I see your __() function definition because I cannot understand how it can be gettext and support 2 arguments.

sandrodz commented 6 years ago

It is a get text wrapper for WordPress: https://codex.wordpress.org/I18n_for_WordPress_Developers

Sorry for confusion!

kylekatarnls commented 6 years ago

I think it's a "feature" of this plugin, see the documentation (and also the file source https://core.trac.wordpress.org/browser/tags/4.8/src/wp-includes/l10n.php#L121):

(string) (optional) Domain to retrieve the translated text
Default: 'default'

What I understand is: if you don't specify, the domain as second argument, it will be replaced by the string "default". So textdomain(txtdomain) is never taken into account. That's seems a bit dummy to me according to gettext standard usage but it can be some easier for small app with only one text domain, so you just have to name this domain "default".

You could do your own wrapper-wrapper:

function ___($text, $domain = null) {
  return __($text, $domain === null ? textdomain() : $domain);
}
function __e($text, $domain = null) {
  return _e($text, $domain === null ? textdomain() : $domain);
}