Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:21:11+00:00 2026-06-13T21:21:11+00:00

I am learning PHP (constantly) and I created some time ago a class that

  • 0

I am learning PHP (constantly) and I created some time ago a class that handles translations. I want to emulate gettext but getting the translated strings from a database. However, now that I see it again, I don’t like that, being a class, to call it I need to use $Translate->text('String_keyword');. I wouldn’t like either to have to use $T->a('String_keyword'); since that’s completely not intuitive.

I’ve been thinking a lot on how to make it to be called with a simple _('String_keyword'), gettext style but, from what I’ve learned from SO, I haven’t been able to find a ‘great’ way to accomplish this. I need to pass somehow the default language to the function, I don’t want to pass it every time I call it as it would be _('String_keyword', $User->get('Language'))). I also don’t want to include the user-detection script in the _() function, as it only needs to be run once and not every time.

The easiest one would be to use GLOBALS, but I’ve learned here that they are completely-utterly forbidden (could this be the only case where I can use them?), then I thought to DEFINE a variable with the user’s language like define ( USER_LANGUAGE , $User->get('Language') ), but it seems just to be the same as a global. These are the 2 main options I can see, I know there are some other ways like Dependency Injection but they seem to add just too much complication for a so simple request and I haven’t yet had time to dig into them.

I was thinking about creating a wrapper first to test it out. Something like this:

function _($Id, $Arg = null)
  {
  $Translate = new Translate (USER_LANGUAGE);
  return $Translate -> text($Id, $Arg)
  }

Here is the translation code. The language is detected before and passed to the object when created.

// Translate text strings
// TO DO: SHOULD, SHOULD change it to PDO! Also, merge the 2 tables into 1
class Translate
  {
  private $Lang;

  function __construct ($Lang)
    {
    $this->Lang = $Lang;
    }

  // Clever. Adds the translation so when codding I don't get annoyed.
  private function add ($Id, $Text)
    {
    $sql="INSERT INTO htranslations (keyword, en, page, last) VALUES ('$Id', '$Text', '".$_SERVER['PHP_SELF']."', now())";

    mysql_query($sql);
    }

  private function retrieve ( $Id )
    {
    $table = is_int ($Id) ? "translations" : "htranslations";      // A small tweak to support the two tables, but they should be merged.
    $results = mysql_query ("SELECT ".mysql_real_escape_string($this->Lang)." FROM ".$table." WHERE keyword='".mysql_real_escape_string($Id)."'");
    $row = mysql_fetch_assoc ($results);
    return mysql_num_rows ($results) ? stripslashes ($row[$this->Lang]) : null;
    }

  // If needed to insert a name, for example, pass it in the $Arg
  public function text($Id, $Arg = null)
    {
    $Text = $this->retrieve($Id);
    if (empty($Text))
      {
      $Text = str_replace("_", " ", $Id);  // If not found, replace all "_" with " " from the input string.
      $this->add($Id, $Text);
      }
    return str_replace("%s", $Arg, $Text);    // Not likely to have more than 2 variables into a single string.
    }
  }

How would you accomplish this in a proper yet simple (for coding) way? Are any of the proposed methods valid or can you come with a better one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:21:15+00:00Added an answer on June 13, 2026 at 9:21 pm

    If the problem is simply that

    $Translate->text('String_keyword');
    

    feels to long, then consider making the Translate object into a Functor by implementing __invoke:

    class Translate
    {
        // all your PHP code you already have
    
        public function __invoke($keyword, $Arg = null)
        {
            return $this->text($keyword, $Arg)
        }
    }
    

    You can then instantiate the object regularly with all the required dependencies and settings and call it:

    $_ = new Translate(/* whatever it needs */);
    echo $_('Hallo Welt');
    

    That would not introduce the same amount of coupling and fiddling with the global scope as you currently consider to introduce through a wrapper function or as the Registry/Singleton solution suggested elsewhere. The only drawback is the non-speaking naming of the object variable as $_().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently learning PHP and MySQL. I'm just wrote a class that handles all
I have been learning PHP for some time now and I wanted one clarification.
Just learning PHP and I'm having some trouble understanding mysql_query. My understanding is that
I am learning php. I want to write string to file, but nothing happens.
I'm a student learning PHP. I basically make the stuff work, but never wondered
i just got some more questions while learning PHP, does php implement any built
I am just learning PHP . i want to display string like 32th meet
I've done a fair bit of PHP over the years but I'm currently learning
I started learning PHP awhile ago. I guess I'm fairly okay in it. I
I am learning PHP by myself for doing my final year project, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.