Skip to content

Instantly share code, notes, and snippets.

@rissajeanne
Created April 4, 2012 18:10
Show Gist options
  • Save rissajeanne/2304338 to your computer and use it in GitHub Desktop.
Save rissajeanne/2304338 to your computer and use it in GitHub Desktop.
<?php
/**
* Loop through translation directory for given locale, find all PO files,
* and generate key/value pairs from ids and strings therein.
*
* @return array
* @params boolean $default
* @author Marissa Hogue
*/
function i18nPOStrings($default = false) {
static $messages = array();
if ($default) {
$locale = get_instance()->config->item('language_ui_default')->url_name;
}
else {
$locale = get_instance()->config->item('language_ui')->url_name;
}
if (!array_key_exists($locale, $messages) || $messages[$locale] === null) {
$strings = array();
$dir = APPPATH . 'language/messages/*/';
$files = glob($dir . '*.' . $locale . '.po');
$msgid = null;
$msgstr = '';
foreach ($files as $file) {
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (preg_match("/^msgid/", $line)) {
if ($msgid) {
$strings[$msgid] = $msgstr;
$msgstr = '';
}
$msgid = preg_replace("/^msgid \"|\"$/", "", $line);
}
elseif (preg_match("/^msgstr/", $line)) {
$msgstr = preg_replace("/^msgstr \"|\"$/", "", $line);
}
$strings[$msgid] = $msgstr;
}
}
$messages[$locale] = $strings;
}
return $messages[$locale];
}
/**
* Get all UI Strings for current locale, replacing empty msgids with default value.
*
* @return array
* @author Marissa Hogue
*/
function getAllUIStrings() {
$messages = array();
$current = i18nPOStrings();
$default = i18nPOStrings(true);
if ($current !== $default) {
$messages = array_merge($default, $current);
} else {
$messages = $default;
}
return $messages;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment