Junior Spellweaver
- Joined
- Dec 29, 2015
- Messages
- 111
- Reaction score
- 75
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);
}
}
}
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);
}
}
}
{
"ME_PROFILE_TITLE" : "Mijn Profiel",
"SETTINGS_TITLE_GENERAL" : "TEST",
"SETTINGS_MOTTO_GENERAL" : "Motto",
"SETTINGS_MOTTO_DESCRIPTION_GENERAL" : "TEST111"
}
I can't include a text.... like include '<bodysadasfsafgas'And what error are you facing?
foreach($obj as $key => $value) {
$file = str_ireplace('{'. $key . '}', $value, $file);
}
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;
<?= $this->languageParser()->USER_NAVIGATION_TITLE; ?>
{USER_NAVIGATION_TITLE} <- That's what I want
<?= $this->languageParser()->USER_NAVIGATION_TITLE; ?> <- That's what I [U]don't[/U] want
Thanks! But I don't want "save / edit" the file. I just want replace the text, where is displaying on the page.I edited my post before you replied- I completely misread the code at first.
Just a simple code:What code do you use to display the html after it's processed?
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;
}
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)
);
}