The Wayback Machine - https://web.archive.org/web/20200705180734/https://github.com/panique/huge/issues/758
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DONE][documentation needed] Dynamic Texts/Feedback Messages using Text::get(); #758

Open
videsignz opened this issue Dec 5, 2015 · 12 comments

Comments

@videsignz
Copy link

@videsignz videsignz commented Dec 5, 2015

Hi Chris,
While expanding on the app, many times I have needed dynamic/user specific text for positive/negative feedback. But I like keeping with using the texts array because its very easy to manage that way.

So with a little thought and only 6 small lines of code, I came up with a super simple way. This does NOT effect anything in the app, only offers the option for customization.

core/text.php
Change this...

public static function get($key)

To this...

public static function get($key, $data=NULL)

Then right below here....

if (!$key) {
    return null;
}

Add this...

if ($data) {
    foreach ($data as $var => $value) {
        ${$var} = $value;
    } 
}

And....viola!

Now in the texts array you can do something like

"FEEDBACK_USER_EMAIL_ALREADY_TAKEN" => "Sorry, the email $user_email is already in use."

And when you add feedback you do it like normal yet add the variable like this

Text::get('FEEDBACK_USER_EMAIL_ALREADY_TAKEN', ['user_email' => $user_email])

This of course will print
_Sorry, the email [email protected] is already in use._

Just like that you can now have dynamic and user specific texts and feedback messages! Also, I am not saying to change the existing text array here in the repository, just the core/text.php class to allow for this feature :)

@panique
Copy link
Owner

@panique panique commented Dec 5, 2015

This is awesome! Thank you very much, this should definitely go into the project! Please gimme some days for testing.

@videsignz
Copy link
Author

@videsignz videsignz commented Dec 5, 2015

Sweet!! I'm happy you like the idea! :)

@slaveek
Copy link
Contributor

@slaveek slaveek commented Dec 5, 2015

Small typo here.

Text::get('FEEDBACK_USER_EMAIL_ALREADY_TAKEN', ['user_email' => $user_email'])

Too many quotation marks ;)

@videsignz
Copy link
Author

@videsignz videsignz commented Dec 5, 2015

@slaveek Haha, good eye!! Thanks man! Fixed :)

@panique
Copy link
Owner

@panique panique commented Dec 19, 2015

Thanks, this is now implemented in dev + master branch, big thanks to @abmmhasan to doing this!

@panique
Copy link
Owner

@panique panique commented Dec 19, 2015

Would be cool if this would also get a little notice in the readme, so people know how to use this feature!

@panique panique changed the title [QUICK FEATURE] Dynamic Texts/Feedback Messages using Text::get(); [DONE][QUICK FEATURE] Dynamic Texts/Feedback Messages using Text::get(); Dec 19, 2015
@panique panique changed the title [DONE][QUICK FEATURE] Dynamic Texts/Feedback Messages using Text::get(); [DONE][FEATURE] Dynamic Texts/Feedback Messages using Text::get(); Dec 19, 2015
@panique
Copy link
Owner

@panique panique commented Dec 19, 2015

Works perfectly btw :)

@videsignz
Copy link
Author

@videsignz videsignz commented Dec 20, 2015

Awesome!! Glad one of my Ideas made it into production :)

@panique panique changed the title [DONE][FEATURE] Dynamic Texts/Feedback Messages using Text::get(); [DONE][documentation needed] Dynamic Texts/Feedback Messages using Text::get(); Jan 5, 2016
@videsignz
Copy link
Author

@videsignz videsignz commented Mar 12, 2016

I'm so bummed. I can not get this working on my local dev setup. Not sure if it's a php setting that I am missing.

@videsignz
Copy link
Author

@videsignz videsignz commented Mar 12, 2016

Ok, so....if I change the core Text class to A and use it like A::get(); it works. Not sure what to do about this one. Seems it is a loading order issue.

@videsignz
Copy link
Author

@videsignz videsignz commented Mar 12, 2016

I isolated the real issue. I know the following is by design, but is there any real downfall to changing it?

Due to the file being loaded once, there is times it loads the texts before the dynamic text variable is assigned.

// load config file (this is only done once per application lifecycle)
if (!self::$texts) {
    self::$texts = require('../application/config/texts.php');
}

With that being said, what would be the downfall of eliminating the check...

if (!self::$texts)
@JOOSTR
Copy link

@JOOSTR JOOSTR commented Mar 26, 2016

You could fix that by doing:

public static function get($key, $data = null)
if ($data) {
return vsprintf(self::$texts[$key], $data);
}

Though then you would have to use it like this:

"FEEDBACK_LOGIN_FAILED" => "Login failed. %s", // Example from texts file
Text::get('FEEDBACK_LOGIN_FAILED', ['because something']);

Output: Login failed. because something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.