Keeping labels/titles consistent

It occurred to me as I was typing out some form checkboxes that the labels I was typing were used in many different areas across the site. I’m working on a site that has user types such as “club_15” or “club_25” which are displayed as “Wine Club Member (15%)” and "Wine Club Member (25%).

It seems like it would be useful to keep an array somewhere, like this:

$user_type_labels = array(
   'club_15' => 'Wine Club Member (15%)',
   'club_25' => 'Wine Club Member (25%)'
);

Is that a common thing to do? Are there other approaches to solving this problem?

Going back to the OPs point and leaving i18n aside.

Lets say on that on a dedicated box running multiple copies of the same app, and the “100 settings max” suggestion applied - is there a role to play for Apache to load these 100 vars into memory on a per domain basis?

I realise may seem like it is unnecessarily separating concerns, making the app non-distibutable etc but, for me it would be no worse than using .htaccess to rewite urls to make controllers do your bidding.

Check out “GNU gettext” (http://en.wikipedia.org/wiki/GNU_gettext).

Do yourself a favor and consider gettext for your next project, its one of the better options for a multilingual website.

Almost any existing solution would be overkill for this task, imho.

Caching? How many strings do you have? Split them into multiple files.

The database is almost certainly not the way to go for something this simple.

This is how I define a language file:


<?php
$lang = LangFactory::getLang();
$lang->add('club_15', 'Wine Club Member (15%)');
?>

It can be split into multiple files to reduce loading unnecessary strings. (I like to keep them simple, because where I use them they’re generated from a client supplied CSV).

If you’re dealing with less than 100 strings here you could easily do this with a simple array() definition in one file.

You both mentioned translation. I have an existing translation class that I use so clients can customize error messages and such through their admin. Would it be appropriate to use that same system for labels? Something like this:

$translator->translate('club_15');

Before I go into too much trouble hacking my class, are there any existing translation libraries I could use? Something that caches values or uses the filesystem for storage would be nice, so I don’t have to hit the database every time I translate a label.

yes you are right deer maintainability is very important.
I am totally agree with your point

Definitely worthwhile if only to improve maintainability (What if one of the labels changes?).

The problem is similar to multilingual sites. You’d have a string table and pull in the relevant string where it’s needed. Generally you’d do something along the lines of $locale->get(‘club_15’); or whatever. Implementation is somewhat irrelevant here but your $locale object would usually also handle things like formatting numbers/currencies/dates, of course that’s beyond what you need it for :slight_smile:

Sure I have seen that done before, I have seen many techniques (some better than others) used to make translation easier.

I personally prefer the use of _() which is an alias for gettext() I believe.

I actually implement my own _T() function and keep translations stored using the MD5 ahsh of the original string, this way my templates and code will read like native English, or at least thats the idea. :stuck_out_tongue:

Cheers,
Alex