Post by Joe1234 » Tue May 10, 2022 11:59 am

Is it possible to use the variables from the language files in some way within the information page or modules? So if there is something that I want displayed on multiple pages I can use the variable in case it needs to be changed at some point I could just change it in one place (language file or extension) instead of multiple places.
Last edited by Joe1234 on Sun May 15, 2022 2:37 pm, edited 1 time in total.

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by ADD Creative » Tue May 10, 2022 5:24 pm

You can load a language file in a controller. For example.

Code: Select all

$this->load->language('common/header');
Just be careful that you don't overwrite a string that using the same name.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by straightlight » Tue May 10, 2022 5:43 pm

Better to use a language Event Trigger, /before, as an extension than to call the $this->load->language in order to avoid variable override.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by paulfeakins » Tue May 10, 2022 6:53 pm

straightlight wrote:
Tue May 10, 2022 5:43 pm
Better to use a language Event Trigger, /before, as an extension than to call the $this->load->language in order to avoid variable override.
How would that avoid conflicting variables?

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by straightlight » Tue May 10, 2022 7:02 pm

paulfeakins wrote:
Tue May 10, 2022 6:53 pm
straightlight wrote:
Tue May 10, 2022 5:43 pm
Better to use a language Event Trigger, /before, as an extension than to call the $this->load->language in order to avoid variable override.
How would that avoid conflicting variables?
By the source of the extension, without conflicting with another one. The only downside is the language definition from the core an extension could still override obviously as opposed to the use of the extension switch case with the $this->load->language.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by paulfeakins » Wed May 11, 2022 7:42 pm

straightlight wrote:
Tue May 10, 2022 7:02 pm
paulfeakins wrote:
Tue May 10, 2022 6:53 pm
straightlight wrote:
Tue May 10, 2022 5:43 pm
Better to use a language Event Trigger, /before, as an extension than to call the $this->load->language in order to avoid variable override.
How would that avoid conflicting variables?
By the source of the extension, without conflicting with another one.
???

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by Joe1234 » Wed May 11, 2022 8:37 pm

I'm a little confused. Isn't that a php code, how do I get that into an html formatted input? Doesn't that load the entire language file to retrieve the string from? I'm trying to get the string from an already accessible language file in something like my information pages, not code the text directly in the twig file.

For example in the text editor i put:
"Blah blah blah {{string}} blah blah blah."

{{string}} would contain the text I'm accessing from the language file that is already loaded that I already edited.

For instance, I have a free shipping dollar amount that I need displayed in different areas (blocks, banners, slider, shipping info page, etc.), that dollar amount may change and I just want to edit it in one place like the language file.

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by by mona » Wed May 11, 2022 11:09 pm

in your language file you have :

Code: Select all

$_['text_model']               = 'Product Code';
$_['text_reward']              = 'Reward Points:';
you can put placeholders in your html like {{ text_model }} and {{ text_reward }}

after you have retrieved your html from the database you would then replace those placeholders with the value from your language files.

Code: Select all

	// example html with placeholders you would normally get from your database
	$your_html_with_placeholders = '<span>{{ text_model }} some other text you want {{ text_reward }} and some more text.</span>';
	if (preg_match_all('/{{(.*?)}}/', $your_html_with_placeholders, $matches)) {
		foreach ($matches[1] as $match) {
			// get the replacement text from the language file for the match
			$replacement = $this->language->get($match);
			// replace it
			$your_html_with_placeholders = str_replace('{{'.$match.'}}',$replacement,$your_html_with_placeholders);
		}
	}
	// print the html with replacements
	error_log($your_html_with_placeholders);
You would however do this replacement everywhere you want them and make sure the delimiters are unique in the text.

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by straightlight » Thu May 12, 2022 8:43 am

To test the intervals with regular expressions: https://github.com/opencart/opencart/bl ... al.php#L78

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Joe1234 » Thu May 12, 2022 10:03 am

Thanks Mona, works perfectly. Now am I able to load two languages into a controller to work at the same time (the regular language and then mine with my strings) so I don't have to possibly insert my strings in 5 different language files to work throughout the shop...and then be in the same predicament of editing something in multiple places? Or is there a master language file that everything calls?

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by by mona » Thu May 12, 2022 9:29 pm

Each language package has a main language file named by the language code for example en-gb.php which has definitions needed site-wide and is loaded at startup.
Then it has additional language files per controller/view which are loaded in the various controllers when needed.
So put your definitions in the main file if you need them in many locations, the replacement code you still have to do in those locations though.

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by Joe1234 » Sun May 15, 2022 2:36 pm

Thanks

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am
Who is online

Users browsing this forum: SohBH and 271 guests