views:

24

answers:

1

To clarify:

  • I'm not using the gettext php module/extension, reason being because I have to deal with servers that don't have gettext included, I'm aware of php-gettext but that just complicates things in that I have to use multiple libraries. Further more I don't want to depend on the user having the necessary locales installed, and some servers I won't have root access to do this. I also realize there are lots of issues with locales in general.
  • I have to support PHP4. Yes, it's dead. Yes, I hate it. But I don't have a choice.

I have a class that parses a "server-level" .mo gettext file based on the current language ( defaults to en ), iterates through every msgid and msgstr and populates an array with keys/values of the msgid and msgstr.

Afterwards I have a method which parses an "application-level" .mo file and merges the two together in the initial array being set, so the application-level translations take precedence over the server-side generic translations.

Since I'm using the merged array for my string translations, and I don't want to read the .mo files on every single page read I'll need some way to actually cache this and use the cached version if the page being read is the same language/locale as the cached version.

So If I went to my website and the default language was english it would read the english server/application mo files, merge them, make the array, cache it. If the next page the user clicked on is English, the cached version will be used. Otherwise, if it was say, Spanish, the merging takes place again and the spanish cached version is used.

So basically, how would I go about caching language/locale specific merged arrays? I've never actually implemented caching myself, but I would assume all I'd need to do is serialize my array, then write it into an application-level cache directory, and save the filename as something like 'translation-{$locale}' where $locale is something like 'en', then use logic to determine what the locale is in my app, based on that see if there's a cached version available - if so, unserialize and just use that?

Is this usually how it's done? I would very much appreciate code examples. The array I have has around 200-300 key/value pairs and is just a two level multidimensional array:

array(
  'en' => array(
  'Hello' => '',
),
  'es' => array(
  'Hello' => 'Hola',
)
);
+1  A: 

Hey,

I'm not understanding why you are caching multiple language arrays? Do you have to provide support for > 1 language during a single request (its php) of your application?

Anyways, what your saying is fine - serialize the array, stick it in the cache and retrieve it in the future. If you cache-miss, recompute the array.

then write it into an application-level cache directory, and save the filename as something like 'translation-{$locale}' where $locale is something like 'en', then use logic to determine what the locale is in my app, based on that see if there's a cached version available.

File I/O is not as fast as memory access - if your going through the trouble of caching this, I would recommend looking into memcache.

Mr-sk
memcached is not feasible because this framework needs to be installed on various types of environments, many of which ( 99% ) do not and probably will not have memcached.
meder
the application supports different locales, some sites have german, english, french translations, each of these locales/languages have server/client mo files which are merged.
meder