Store locales' meta info in the database

This commit is contained in:
Dmitriy Simushev 2015-05-21 12:39:36 +00:00
parent 5d13afd8f6
commit 137d92624e
5 changed files with 238 additions and 62 deletions

View File

@ -129,12 +129,23 @@ translation:
# Contains locales info
locale:
fields:
# Artificial primary key
# Artificial primary key.
localeid: "int NOT NULL auto_increment PRIMARY KEY"
# Locale code
code: "varchar(5) NOT NULL"
# Indicates if a locale is enabled or not.
# Human readable name of the locale.
name: "varchar(128) NOT NULL DEFAULT ''"
# Indicates if the locale is enabled or not.
enabled: "tinyint NOT NULL DEFAULT 0"
# Indicates if the locale uses RTL writing.
rtl: "tinyint NOT NULL DEFAULT 0"
# Name of the locale which is used with PHP's setlocale to format dates.
time_locale: "varchar(128) NOT NULL DEFAULT 'en_US'"
# Serialized array of various date formats.
date_format: "text"
unique_keys:
# Make sure locale code can be duplicated
code: [code]
# Contains localized mail templates
mailtemplate:

View File

@ -714,26 +714,8 @@ class Installer
foreach ($locales as $locale_info) {
$locale = $locale_info['code'];
// Import localized messages
import_messages(
$locale,
MIBEW_FS_ROOT . '/locales/' . $locale . '/translation.po',
true
);
// Import canned messages
$canned_messages_file = MIBEW_FS_ROOT . '/locales/' . $locale
. '/canned_messages.yml';
if (is_readable($canned_messages_file)) {
import_canned_messages($locale, $canned_messages_file);
}
// Import mail templates
$mail_templates_file = MIBEW_FS_ROOT . '/locales/' . $locale
. '/mail_templates.yml';
if (is_readable($mail_templates_file)) {
MailUtils::importTemplates($locale, $mail_templates_file);
}
// Import translations, formats, mail templates, ...
import_locale_content($locale);
// Mark the locale as "enabled" to indicate that all its content
// is imported.

View File

@ -317,4 +317,80 @@ class Updater
return true;
}
/**
* Performs all database updates needed for 2.1.0.
*
* @return boolean True if the updates have been applied successfully and
* false otherwise.
*/
protected function update20100()
{
$db = $this->getDatabase();
if (!$db) {
return false;
}
// Alter locale table.
try {
$db->query('ALTER TABLE {locale} ADD COLUMN name varchar(128) NOT NULL DEFAULT "" AFTER code');
$db->query('ALTER TABLE {locale} ADD COLUMN rtl tinyint NOT NULL DEFAULT 0');
$db->query('ALTER TABLE {locale} ADD COLUMN time_locale varchar(128) NOT NULL DEFAULT "en_US"');
$db->query('ALTER TABLE {locale} ADD COLUMN date_format text');
$db->query('ALTER TABLE {locale} ADD UNIQUE KEY code (code)');
} catch (\Exception $e) {
$this->errors[] = getlocal('Cannot update tables: {0}', $e->getMessage());
return false;
}
try {
// Store configs for available locales in the database.
$locales = $db->query(
'SELECT localeid as id, code from {locale}',
null,
array('return_rows' => Database::RETURN_ALL_ROWS)
);
$locales_info = get_locales();
foreach ($locales as $row) {
$id = $row['id'];
$code = $row['code'];
$info = (isset($locales_info[$code]) ? $locales_info[$code] : array())
// Default info
+ array(
'name' => $code,
'rtl' => false,
'time_locale' => 'en_US',
'date_format' => array(
'full' => '%d %B %Y, %H:%M',
'date' => '%d %B %Y',
'time' => '%H:%M',
),
);
$db->query(
('UPDATE {locale} SET '
. 'name = :name, rtl = :rtl, time_locale = :time_locale, '
. 'date_format = :date_format '
. 'WHERE localeid = :id'),
array(
':id' => $id,
':name' => $info['name'],
':rtl' => $info['rtl'] ? 1 : 0,
':time_locale' => $info['time_locale'],
':date_format' => serialize($info['date_format']),
)
);
}
} catch (\Exception $e) {
$this->errors[] = getlocal('Cannot update content: {0}', $e->getMessage());
return false;
}
return true;
}
}

View File

@ -20,6 +20,7 @@
use Mibew\Database;
use Mibew\Mail\Utils as MailUtils;
use Symfony\Component\Translation\Loader\PoFileLoader;
use Symfony\Component\Yaml\Parser as YamlParser;
/**
* Name for the cookie to store locale code in use
@ -245,17 +246,11 @@ function get_locale_links()
/**
* Returns meta data for all known locales.
*
* This function is deprecated. Use {get_locale_info()} instead.
*
* @deprecated since 2.1.0
* @return array Associative arrays which keys are locale codes and the values
* are locales info. Locale info itself is an associative array with the
* following keys:
* - name: string, human readable locale name.
* - rtl: boolean, indicates with the locale uses right-to-left
* writing mode.
* - time_locale: string, locale code which is used in {@link setlocale()}
* function to set the correct date/time formatting.
* - date_format: array, list of available date formats. Each key of the
* array is format name and each value is a format string for
* {@link strftime()} function.
* are locales info.
*/
function get_locales()
{
@ -696,19 +691,61 @@ function get_locales()
/**
* Returns locale info by its code.
*
* It is a wrapper for {@link get_locales()} function and can be used to improve
* readability of the code.
*
* @param string $locale
* @return array|false Associative array of locale info or boolean false if the
* locale is unknown. See {@link get_locales()} description for details of the
* info array keys.
* locale is unknown. Locale info array contains the following keys:
* - name: string, human readable locale name.
* - rtl: boolean, indicates with the locale uses right-to-left
* writing mode.
* - time_locale: string, locale code which is used in {@link setlocale()}
* function to set the correct date/time formatting.
* - date_format: array, list of available date formats. Each key of the
* array is format name and each value is a format string for
* {@link strftime()} function.
*/
function get_locale_info($locale)
{
$locales = get_locales();
static $cache = array();
return isset($locales[$locale]) ? $locales[$locale] : false;
if (!isset($cache[$locale])) {
if (get_maintenance_mode() === false) {
// Load local info from the database
$info = Database::getInstance()->query(
'SELECT * FROM {locale} WHERE code = :code',
array(':code' => $locale),
array('return_rows' => Database::RETURN_ONE_ROW)
);
$cache[$locale] = $info
? array(
'name' => $info['name'],
'rtl' => (bool)$info['rtl'],
'time_locale' => $info['time_locale'],
'date_format' => unserialize($info['date_format'])
)
: false;
} else {
// Either installation or update is performed. Try to get locale
// info from its config file.
$config_path = MIBEW_FS_ROOT . '/locales/' . $locale . '/config.yml';
$info = read_locale_config($config_path);
$cache[$locale] = $info
? $info + array(
'name' => $locale,
'rtl' => false,
'time_locale' => 'en_US',
'date_format' => array(
'full' => '%B %d, %Y %I:%M %p',
'date' => '%B %d, %Y',
'time' => '%I:%M %p',
),
)
: false;
}
}
return $cache[$locale];
}
/**
@ -926,7 +963,6 @@ function save_message($locale, $key, $value)
* Enables specified locale.
*
* @param string $locale Locale code according to RFC 5646.
* @todo Rewrite the function and move somewhere locale creation and its import.
*/
function enable_locale($locale)
{
@ -944,34 +980,17 @@ function enable_locale($locale)
if ($count == 0) {
// The locale does not exist in the database. Create it.
$db->query(
"INSERT INTO {locale} (code, enabled) VALUES (:code, :enabled)",
Database::getInstance()->query(
('INSERT INTO {locale} (code, enabled) VALUES(:code, :enabled)'),
array(
':code' => $locale,
':enabled' => 1,
)
);
// Import localized messages to the just created locale
import_messages(
$locale,
MIBEW_FS_ROOT . '/locales/' . $locale . '/translation.po',
true
);
// Import canned messages for the locale if they exist in the locale's
// files.
$canned_messages_file = MIBEW_FS_ROOT . '/locales/' . $locale . '/canned_messages.yml';
if (is_readable($canned_messages_file)) {
import_canned_messages($locale, $canned_messages_file);
}
// Import mail templates for the locale if they exist in the locale's
// files.
$mail_templates_file = MIBEW_FS_ROOT . '/locales/' . $locale . '/mail_templates.yml';
if (is_readable($mail_templates_file)) {
MailUtils::importTemplates($locale, $mail_templates_file);
}
// Import all locale-related info (translations, mail, templates,
// formats...) to databse.
import_locale_content($locale);
} else {
// The locale exists in the database. Update it.
$db->query(
@ -999,3 +1018,80 @@ function disable_locale($locale)
)
);
}
/**
* Imports all locale's content (messages, mail templates, configs) to database.
*
* This function does not create the locale in database so you have to create it
* by yourself.
*
* @param string $locale Code of the locale to import.
*/
function import_locale_content($locale)
{
$config = (read_locale_config(MIBEW_FS_ROOT . '/locales/' . $locale . '/config.yml') ?: array())
+ array(
'name' => $locale,
'rtl' => false,
'time_locale' => 'en_US',
'date_format' => array(
'full' => '%B %d, %Y %I:%M %p',
'date' => '%B %d, %Y',
'time' => '%I:%M %p',
),
);
Database::getInstance()->query(
('UPDATE {locale} SET '
. 'name = :name, rtl = :rtl, time_locale = :time_locale,'
. 'date_format = :date_format '
. 'WHERE code = :code'),
array(
':code' => $locale,
':name' => $config['name'],
':rtl' => $config['rtl'] ? 1 : 0,
':time_locale' => $config['time_locale'],
':date_format' => serialize($config['date_format'])
)
);
// Import localized messages to the just created locale
import_messages(
$locale,
MIBEW_FS_ROOT . '/locales/' . $locale . '/translation.po',
true
);
// Import canned messages for the locale if they exist in the locale's
// files.
$canned_messages_file = MIBEW_FS_ROOT . '/locales/' . $locale . '/canned_messages.yml';
if (is_readable($canned_messages_file)) {
import_canned_messages($locale, $canned_messages_file);
}
// Import mail templates for the locale if they exist in the locale's
// files.
$mail_templates_file = MIBEW_FS_ROOT . '/locales/' . $locale . '/mail_templates.yml';
if (is_readable($mail_templates_file)) {
MailUtils::importTemplates($locale, $mail_templates_file);
}
}
/**
* Reads locale's config files.
*
* @param string $path Path of the file to read.
* @return boolean|array Boolean false if the file is not found and associative
* configs array otherwise.
*/
function read_locale_config($path)
{
if (!is_readable($path)) {
return false;
}
$parser = new YamlParser();
$config = $parser->parse(file_get_contents($path));
return $config;
}

View File

@ -0,0 +1,11 @@
# This is human-readable name which will be used everywhere in UI.
name: English
# Indicates if the locale uses Right-to-Left writing.
rtl: false
# Locale name which will be passed into PHP's setlocale function.
time_locale: en_US
# Various formats which will be used with PHP's strftime function.
date_format:
full: "%B %d, %Y %I:%M %p"
date: "%B %d, %Y"
time: "%I:%M %p"