/    Sign up×
Community /Pin to ProfileBookmark

Simple language file function

I am trying to create a basic language file for my website.

I have included all words in a language file and they are all in a $lang[] array:

[code]
$lang[‘about’] = ‘About us’;
$lang[‘privacy’] = ‘Privacy’;
$lang[‘sitemap’] = ‘Sitemap’;
[/code]

Instead of using echo all the time, I wanted a function I could use to point to the language file and print the required line by using something like: myText(‘about’); which would print: About us.

Something like:

[code]
function myText($line){
$lang[0] = $line;
echo $lang[0];
}
// At the moment a call with myText(‘about’) will return about, NOT About us.
[/code]

Any ideas?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarFeb 25.2009 — [code=php]
class local{
private $locals;
public function __construct($lang){
$this->locals = file($lang);
}
public function __get($line){
return $this->locals[ (int) $line];
}
}


$l = new local('en.txt');
echo "line one say's: '{$l->0}'";[/code]
Copy linkTweet thisAlerts:
@paperclipauthorMar 02.2009 — Thanks scragar for the suggestion.

After more searching, I've hacked together this function which works quite well for me. Granted it's not exactly fully featured but does what I want it to do. I'd welcome further insight or improvements.

[code=php]
function myText($line) {
global $lang;

if (isset($lang[$line])) {
echo $lang[$line];
} else {
echo 'missing_lang_entry_in_array';
}
}

[/code]


[code=php]
<?php myText('about'); ?> // Prints: About us
[/code]
×

Success!

Help @paperclip spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...