Jump to content

Translation in Hook understanding


wp4all

Recommended Posts

Hi @ all,

I would add 2 things in a Hook but I guess it is wrong due to a lack of understanding.

<?php

use WHMCS\User\Alert;

add_hook('ClientAlert', 1, function($client) {
    $salutation = $client->custom_fields.1;
    $firstName = $client->firstName;
    $lastName = $client->lastName;
    return new Alert(
        "Hallo {$salutation} <b>{$firstName} {$lastName}</b><br>
        Here I would add text as $LANG but the clasic {$lang.infotext} is not working ",
        'info' 
    );
});

I have an custom_field.1 which includes the salutation Mr/Mrs but it will not displayed in the hook firstName & lastName works fine.

The next Problem I do not know exactly how to add $_LANG['infotext'] = "Here my text"; as a {$lang.infotext} in to a hook.

Greetings Christian

 

Link to comment
Share on other sites

26 minutes ago, wp4all said:

The next Problem I do not know exactly how to add $_LANG['infotext'] = "Here my text"; as a {$lang.infotext} in to a hook.

You can use Lang::trans('infotext') within the hook.

26 minutes ago, wp4all said:

I have an custom_field.1 which includes the salutation Mr/Mrs but it will not displayed in the hook firstName & lastName works fine.

This should help: WHMCS Internal Class Documentation.

Edited by string
Link to comment
Share on other sites

Hi Christian,

57 minutes ago, wp4all said:

I would add 2 things in a Hook but I guess it is wrong due to a lack of understanding.

the problem is that you're treating it like Smarty with the {$salutation} in the output - you need to think in PHP.

57 minutes ago, wp4all said:

I have an custom_field.1 which includes the salutation Mr/Mrs but it will not displayed in the hook firstName & lastName works fine.

you'll need to refer to the Class docs to see how to access the customfield values...

https://docs.whmcs.com/classes/7.6/WHMCS/User/Client.html#method_customFieldValues

or query the db for it if that's easier for you.

57 minutes ago, wp4all said:

The next Problem I do not know exactly how to add $_LANG['infotext'] = "Here my text"; as a {$lang.infotext} in to a hook.

if it were me, i'd make that output into a variable and then output the variable...

$fullname = $client->fullName;
$alertstring = "Hallo ".$salutation."<b>".$fullName."</b>".Lang::trans(infotext);
return new Alert($alertstring);

$salutation would be wrong until you get the customfield sorted, but the rest should work.

Link to comment
Share on other sites

Hi Brian,

you are right with the output as variable also with the db query.

Just a last Question how would you now translate the .$salutation->value. ?

Hook:

<?php

use WHMCS\User\Alert;
use Illuminate\Database\Capsule\Manager as Capsule;
if ($_SESSION['uid']) {
add_hook('ClientAlert', 1, function($client) {
$fullname = $client->fullName;
$salutation = Capsule::table('tblcustomfieldsvalues')->where('fieldid','2')->where('relid', $_SESSION['uid'])->first();
$alertstring = "Hallo " .$salutation->value."</b> ".$fullname."</b>, " .Lang::trans(infobox);
return new Alert($alertstring);
});
}

It looks now :

image.png.acb55694a12cd9b5ed7c9152711ad5db.png

also the HTML is not working usually in the first row should be 2 x <br> 🙄

Greetings Christian

Link to comment
Share on other sites

19 hours ago, wp4all said:

Just a last Question how would you now translate the .$salutation->value. ?

in this instance, there should only be one value found by the query, so you could use value instead...

<?php

use WHMCS\User\Alert;
use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('ClientAlert', 1, function($client) {
	$client = Menu::context('client');
	$salutation = Capsule::table('tblcustomfieldsvalues')->where('fieldid','57')->where('relid',$client->id)->value('value');
	$alertstring = "Hallo ".$salutation."<b> ".$client->fullName."</b>,<br><br>".Lang::trans('infobox');
	$alertlevel = "info";
	$alertlink = "http://www.google.com";
	return new Alert($alertstring,$alertlevel,$alertlink);
});

you don't need to check if they're a client before running the hook as that hook only works in the clientarea, so that check has effectively already been done..

19 hours ago, wp4all said:

also the HTML is not working usually in the first row should be 2 x <br>

i've added them back in for you - this is how it would look in Six (obviously your custom will be slightly different)...

kipY7Ek.png

... also added how to assign alert severity levels (defaults to using Info - so if not changing from that, you don't need to define/use it) and links (optional) to the hook too. thanks.png

Link to comment
Share on other sites

Hi Brian,

Ok thanks understand, but how should I translate the 

.$salutation.

It will be only shown in the standard Language as the input was selected there is no translation function for custom fields in the backend.

I' m not sure if I understand this in the right sentence

21 minutes ago, brian! said:

in this instance, there should only be one value found by the query, so you could use value instead...

greetings Christian

Edited by wp4all
Link to comment
Share on other sites

57 minutes ago, wp4all said:

Ok thanks understand, but how should I translate the $salutation It will be only shown in the standard Language as the input was selected there is no translation function for custom fields in the backend.

oh ok, i'd forgotten our conversation in May 2018 about this... so looking back, I can recall creating two new language strings in an override file...

$_LANG['Herr'] = "Mr";
$_LANG['Frau'] = "Mrs";

and then in the hook we can use the relevant language string by using the variable inside a Lang::trans

$alertstring = Lang::trans('hello')." ".Lang::trans($salutation)."<b> ".$client->fullName."</b>,<br><br>".Lang::trans('infobox');

if you're going to want this multilingual, i've added the language string for "Hello" which already exists in the language files... so English users will see "Hello", Germans will see "Hallo" etc.

T1hNfiR.png

now i'm not currently making any checks that the language string exists for Frau/Herr - without such checks (which I used in the MarketConnect hooks previously posted I think), if the language override doesn't exist, then it would output the value of $salutation, e.g 'Herr' or 'Frau'.

1 hour ago, wp4all said:

I'm not sure if I understand this in the right sentence

using ->first will give you an entire row from the database, but as you only want one specific value from that row, then you would use ->value instead.... you could use ->first if you wanted to, but then you'd have to add more coding to use the value you want, whereas this way, you can just use $salutation.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated