How to use a "language" file to replace text in web pages?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

How to use a "language" file to replace text in web pages?

Post by Sinemacula »

I'm creating a small report application in PHP and have just realized that there are a lot of terms and phrases that I use over an over, many of them on every page. Now if I decide to change the phrasing, I'm going to have to change it on every page :(

I'm aware of some php/mysql applications that use language files (.ini or .mo & .po) to keep these sorts of things in a one place, allowing for a "change once, it changes everywhere" approach.

Is there a quick/easy way to implement something like this?

Can I just create a .ini file and then call it in the header file for the application? If so, what would I then put in place of the phrases on the actual pages in order to tell the system "put the .ini file phrase for 'x' here" ?

Thanks,
Scott
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: How to use a "language" file to replace text in web pages?

Post by superdezign »

Depends on what these phrases are. Like, if it's the title of the website, I just create some sort of config variable that has the website title in it so that I can easily use the framework for another website. Otherwise, I don't see a reason to do so.
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Re: How to use a "language" file to replace text in web pages?

Post by Sinemacula »

I wasn't thinking of the title of the website, or other similar things (like footer copyright, etc)

It's particular phrases and technical terms that get used in multiple places throughout the report - and therefore are used in several of the php files that make up the report.

For example, one section of the report has the heading "INcongruence: How much difference is there between your conscious and unconscious attitudes?"

That one phrase is in four or five different php files (so far), and it occurred to me that I might want to change the wording. There are many more such phrases and terms that similarly are in several php files, and I may decide I'd like to reword some of these - rather than the alternative which is to check every php file to make the changes manually.

So, my thought was that it would be a LOT easier to have such terms and phrases contained in a single language or locale file so that I could change them in a single place and have that automatically change them on the displayed pages every time they're used.

I'm hoping there's an easy way to implement something like this - it's sort of like css for text ;-)

I know I could create a database table with all of the terms in it, and then use a database query and echo statements to do what I want - but I thought since I've seen packages like Joomla! and LimeSurvey and the like use language files to achieve this, perhaps there's an easy way to implement it for my needs.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: How to use a "language" file to replace text in web pages?

Post by Stryks »

You could possibly create a file like, language.php, and just define some phrases. Then include on every page.

Code: Select all

<?php
define("TEXT_INCONGRUENCE", "INcongruence: How much difference is there between your conscious and unconscious attitudes?");
?>
Then to insert into a page, simply echo the value.

Code: Select all

<p>Current Section: <?php echo TEXT_INCONGRUENCE; ?></p>
While not really the 'best' of methods, it'll work without a lot of hassle. You change the text in the language.php file and it changes everywhere you you echo the value.

But yeah ... possibly even this is more hassle than it's worth.
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Re: How to use a "language" file to replace text in web pages?

Post by Sinemacula »

Thanks Stryks - that's just the kind of thing I was looking for!

I agree that if I only had one or two places where I had to do the replacement it would be more hassle than it's worth - but I don't know how many I'll end up with, or how many iterations I'll go through before I decide on the final wording of everything, so this minimal hassle approach should work nicely!
Post Reply