diff --git a/includes/salsa_entity.entity.inc b/includes/salsa_entity.entity.inc index d25df5b..e07415b 100644 --- a/includes/salsa_entity.entity.inc +++ b/includes/salsa_entity.entity.inc @@ -106,7 +106,7 @@ class SalsaEntity extends Entity { // The language code from salsa is in ISO-693-3, drupal language is in // ISO-693 - this means we need to convert the 2-char drupal language // into a 3-char language. - $iso693_3_langcode = SalsaEntityLanguageMapper::toIso693_3($langcode); + $iso693_3_langcode = SalsaEntityLanguageMapper::toIso639_2($langcode); if (!isset(self::$translations[$iso693_3_langcode][$this->getSalsaType()][$this->identifier()])) { self::$translations[$iso693_3_langcode][$this->getSalsaType()][$this->identifier()] = $this->loadTranslation($iso693_3_langcode); diff --git a/includes/salsa_entity.language_mapper.inc b/includes/salsa_entity.language_mapper.inc index 1730a59..9a53987 100644 --- a/includes/salsa_entity.language_mapper.inc +++ b/includes/salsa_entity.language_mapper.inc @@ -6,60 +6,69 @@ */ /** - * Static helper class to get the ISO-693-3 language code from a given ISO-693-2. + * Static helper class. + * + * Gets the ISO-639-2 language code from a given ISO-639-1 and vice versa. */ class SalsaEntityLanguageMapper { /** - * Mapping of language codes, keyed by ISO-693-2. + * Mapping of language codes, keyed by ISO-639-1. * * @var array */ - protected static $code = array( + protected static $code_mapping = array( 'de' => 'deu', 'en' => 'eng', 'fr' => 'fre', 'it' => 'ita', 'es' => 'spa', 'ru' => 'rus', + 'ca' => 'cat', + 'ar' => 'ara', + 'ku' => 'kur', + 'pt' => 'por', ); /** - * Returns a ISO-693-3 code for the requested language. + * Returns a ISO-639-2 code for the requested language. * * @param string $langcode - * (optional) ISO-693-2 Code language - 2-char, defaults to the current + * (optional) ISO-639-1 Code language - 2-char, defaults to the current * language. * * @return string|null - * The ISO-693-3 or NULL if the language is not defined yet. + * The ISO-639-2 or NULL if the language is not defined yet. */ - public static function toIso693_3($langcode = NULL) { + public static function toIso639_2($langcode = NULL) { global $language; if (!isset($langcode)) { $langcode = $language->language; } + // Provide possibility to override code mapping. + $codes = variable_get('salsa_entity_supporter_language_code_mapping', self::$code_mapping); - if (isset(self::$code[$langcode])) { - return self::$code[$langcode]; + if (isset($codes[$langcode])) { + return $codes[$langcode]; } return NULL; } - /** - * Returns a ISO-693-3 code for the requested language. + * Returns a ISO-639-1 code for the requested language. * * @param string $langcode - * ISO-693-3 Code language - 3-char. + * ISO-639-2 Code language - 3-char. * * @return string|null - * The ISO-693-2 or NULL if the language is not defined yet. + * The ISO-639-1 or NULL if the language is not defined yet. */ - public static function fromIso693_3($langcode) { - if ($key = array_search($langcode, self::$code)) { - return self::$code[$key]; + public static function fromIso639_2($langcode) { + // Provide possibility to override code mapping. + $codes = variable_get('salsa_entity_supporter_language_code_mapping', self::$code_mapping); + if ($key = array_search($langcode, $codes)) { + return $codes[$key]; } return NULL; } diff --git a/salsa_entity.module b/salsa_entity.module index 8f39734..f807222 100644 --- a/salsa_entity.module +++ b/salsa_entity.module @@ -232,6 +232,25 @@ function salsa_entity_salsa_object_type_info() { } /** + * Implements hook_entity_presave(). + * + * Necessary to set the language property on supporter objects upon saving and + * updating. + */ +function salsa_entity_entity_presave($entity, $type) { + if ($type == 'salsa_supporter' && variable_get('salsa_entity_set_supporter_language', TRUE)) { + // Set the code only for new supporters and such that don't have any + // language code yet. + if (empty($entity->supporter_KEY) && empty($entity->Language_Code)) { + global $language; + $lang_code = SalsaEntityLanguageMapper::toIso639_2(); + $default_language = variable_get('language_default'); + $entity->Language_Code = $lang_code ? $lang_code : SalsaEntityLanguageMapper::toIso639_2($default_language->language); + } + } +} + +/** * A callback that returns at list of possible values for the property. */ function salsa_entity_property_info_options($name, $info) {