Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Language System - replace strings

Junior Spellweaver
Joined
Dec 29, 2015
Messages
111
Reaction score
75
Hey guys!
I want to replace all strings from a file and display it, but I don't know how I should replace the strings. That's my current code:

View.php (just a part)
Code:
  public function languageParser() {
        $lang = Config::get(['lang']);
        $LangContent = (!file_exists(DS.'application/langs/'.$lang.'.json')) ? exit('Language File not found') : file_get_contents(DS.'application/langs/'.$lang.'.json');
        $obj = json_decode($LangContent, true);
        $file = file_get_contents($this->file);


        foreach($obj as $o => $value) {
            if(strpos($file, '{'. $o . '}') !== false) {
                str_ireplace('{'. $o . '}', $value, $file);
            }
        }
  }

If I do that:

Code:
  public function languageParser() {
        $lang = Config::get(['lang']);
        $LangContent = (!file_exists(DS.'application/langs/'.$lang.'.json')) ? exit('Language File not found') : file_get_contents(DS.'application/langs/'.$lang.'.json');
        $obj = json_decode($LangContent, true);
        $file = file_get_contents($this->file);


        foreach($obj as $o => $value) {
            if(strpos($file, '{'. $o . '}') !== false) {
                $this->file = str_ireplace('{'. $o . '}', $value, $file);
            }
        }
  }
I'll get an error, because I include the file, so I can't include just a text. What's the best way to change the values?
One of my language files (just a test):
Code:
{


  "ME_PROFILE_TITLE" : "Mijn Profiel",


  "SETTINGS_TITLE_GENERAL" : "TEST",
  "SETTINGS_MOTTO_GENERAL" : "Motto",
  "SETTINGS_MOTTO_DESCRIPTION_GENERAL" : "TEST111"
}

I hope you can help me.
- Kylon
 
Pee Aitch Pee
Joined
Mar 30, 2011
Messages
630
Reaction score
422
And what error are you facing?
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
In your code I'm either not looking close enough, or you're not using include, you are using file_get_contents, which works perfectly fine retrieving a JSON/text file. Also, you don't need to check if there is a string position of the key before replacing that key with a value- that is done for you in the replacing function- and if caps aren't important to you, you should just replace it.
PHP:
foreach($obj as $key => $value) {
    $file = str_ireplace('{'. $key . '}', $value, $file);
}

And you can save the file with file_put_contents(...)
 
Last edited:
Junior Spellweaver
Joined
Dec 29, 2015
Messages
111
Reaction score
75
In your code I'm either not looking close enough, or you're not using include, you are using file_get_contents, which works perfectly fine retrieving a JSON file. You are also parsing the JSON- so you won't find any '{' in your foreach loop. Look in the docs about parsing JSON, but I'm pretty sure the result is either an associative array or an object, so you should be able to play with the data like this:
PHP:
echo $obj['ME_PROFILE_TITLE'];
// or 
echo $obj->ME_PROFILE_TITLE;
Kylon - Language System - replace strings - RaGEZONE Forums

edit: I can echo the data e.g.
Code:
<?= $this->languageParser()->USER_NAVIGATION_TITLE; ?>
I just want replace the string without using PHP in HTML.
Code:
{USER_NAVIGATION_TITLE} <- That's what I want
<?= $this->languageParser()->USER_NAVIGATION_TITLE; ?> <- That's what I [U]don't[/U] want
 
Junior Spellweaver
Joined
Dec 29, 2015
Messages
111
Reaction score
75
I edited my post before you replied- I completely misread the code at first.
Thanks! But I don't want "save / edit" the file. I just want replace the text, where is displaying on the page.
That's my problem. >.>
 
Junior Spellweaver
Joined
Dec 29, 2015
Messages
111
Reaction score
75
What code do you use to display the html after it's processed?
Just a simple code:
Code:
	public function display($file) {
		if(!file_exists($file = DS.'application/views/'.$file.'.tpl')) {
			exit($file.' not found!');
		}


		$this->file = $file;
		$this->languageParser(); //before displaying ### fix
		include_once $this->file;
	}
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Here's a starting point I would suggest:

PHP:
    private $languageFilePath = 'application/langs/';
    private $templateFilePath = 'application/views/';
    private function getLangJSON() {
        return json_decode(file_get_contents(
            DS .
            $this->languageFilePath .
            Config::get(['lang']) .
            '.json'
        ));
    }
    
    private getTemplate ($file) {
        $file = DS . $this->templateFilePath . $file . '.tpl';
        if(!file_exists($file)) {
            exit($file . ' not found!');
        }
        return file_get_contents($file);
    }

    private function parseTemplate ($tpl) {
        $lang = $this->getLangJSON();
        $keys = [];
        $vals = [];
        foreach ($lang as $key => $val) {
            $keys[] = $key;
            $vals[] = $val;
        }
        return str_ireplace($keys, $vals, $tpl);
    }

    public function display($file) {
        echo $this->parseTemplate(
            $this->getTemplate($file)
        );
    }

Edit: Sorry I moved so many things around, I get OCD like that :) - Hope I didn't introduce any little bugs like syntax/reference errors :)
 
Last edited:
Joined
Sep 8, 2011
Messages
822
Reaction score
129
I can't understand why would anyone go through all this trouble..
I made a small language system that determines the language by a user cookie, should the cookie not be present or not match any language setting, it will use the application's config language key, should this not work as well, it will use a fallback locale set in the config.
In my language system, you can pass variables into the string, say your string of 'welcome' key will look like this: 'welcome %PARAM%, how are you?', then you simply pass the name to the function, usage explanation is provided in the repository, feel free to rip it and use it any way you want.


Do note, this is mostly procedural, it's written in partial OOP and the application overall is poop, it was a mere prototype so yeah, it does suck but the language system is awesome.

If you wish to participate in an open source community-driven PHP web framework project (which is for fun, learning and more tailored specific usage for our needs), you can take a look and participate here:

This one is fully OOP, and although being the third prototype of this framework/application, it's my first application ever so I'm quite proud of that :p
 
Back
Top