Skip to content

Commit

Permalink
Updates to lang api to fix a couple of issues introduced by language …
Browse files Browse the repository at this point in the history
…changes

Note:

1) We now define custom_strings_inc.php as a config entry in it's own right
2) custom strings file can use new array format
3) plugin's fallback to english in case where they did not before.
  • Loading branch information
mantis committed Sep 30, 2010
1 parent 4d42be0 commit 57a8ee8
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 149 deletions.
12 changes: 12 additions & 0 deletions config_defaults_inc.php
Expand Up @@ -190,6 +190,18 @@
*/
$g_language_path = $g_absolute_path . 'lang' . DIRECTORY_SEPARATOR;

/**
* absolute path to custom strings file.
* This file allows overriding of strings declared in the language file, or in plugin language files
* Two formats are supported:
* Legacy format: $s_*
* New format: define a $s_custom_messages array as follows:
* $s_custom_messages = array( 'en' => array( string => string ) ) ;
* NOTE: you can not mix/merge old/new formats within this file.
* @global string $g_custom_strings_file
*/
$g_custom_strings_file = $g_absolute_path . 'custom_strings_inc.php';

/**
* Used to link to manual for User Documentation.
* @global string $g_manual_url
Expand Down
8 changes: 5 additions & 3 deletions core/error_api.php
Expand Up @@ -382,14 +382,16 @@ function error_build_parameter_string( $p_param, $p_showtype = true, $p_depth =
function error_string( $p_error ) {
global $g_error_parameters;

$MANTIS_ERROR = lang_get( 'MANTIS_ERROR' );

# We pad the parameter array to make sure that we don't get errors if
# the caller didn't give enough parameters for the error string
$t_padding = array_pad( array(), 10, '' );

$t_error = $MANTIS_ERROR[$p_error];
$t_error = lang_get( $p_error, null, false );

if( $t_error == '' ) {
return lang_get( 'missing_error_string' ) . $p_error;
}

# ripped from string_api
$t_string = call_user_func_array( 'sprintf', array_merge( array( $t_error ), $g_error_parameters, $t_padding ) );
return preg_replace( "/&(#[0-9]+|[a-z]+);/i", "&$1;", @htmlspecialchars( $t_string, ENT_COMPAT, 'UTF-8' ) );
Expand Down
74 changes: 55 additions & 19 deletions core/lang_api.php
Expand Up @@ -66,6 +66,8 @@ function lang_load( $p_lang, $p_dir = null ) {
return;
}

// Step 1 - Load Requested Language file
// @@ and if file doesn't exist???
if( $p_dir === null ) {
include_once( config_get( 'language_path' ) . 'strings_' . $p_lang . '.txt' );
} else {
Expand All @@ -74,23 +76,45 @@ function lang_load( $p_lang, $p_dir = null ) {
}
}

# Allow overriding strings declared in the language file.
# custom_strings_inc.php can use $g_active_language
$t_custom_strings = config_get( 'absolute_path' ) . 'custom_strings_inc.php';
// Step 2 - Allow overriding strings declared in the language file.
// custom_strings_inc.php can use $g_active_language
// 2 formats:
// $s_* - old format
// $s_custom_strings array - new format
// NOTE: it's not expected that you'd mix/merge old/new formats within this file.
$t_custom_strings = config_get( 'custom_strings_file' ) ;
if( file_exists( $t_custom_strings ) ) {
require( $t_custom_strings );

# this may be loaded multiple times, once per language
require( $t_custom_strings );
}


// Step 3 - New Language file format
// Language file consists of an array
if( isset( $s_messages ) ) {
// lang strings array entry can only be set if $p_dir is not null - i.e. in a plugin
if( isset( $g_lang_strings[$p_lang] ) ) {
$g_lang_strings[$p_lang] = array_merge( ((array)$g_lang_strings[$p_lang]), (array)$s_messages);
if( isset( $s_custom_messages[$p_lang] ) ) {
// Step 4 - handle merging in custom strings:
// Possible states:
// 4.a - new string format + new custom string format
$g_lang_strings[$p_lang] = array_replace( ((array)$g_lang_strings[$p_lang]), (array)$s_messages, (array)$s_custom_messages[$p_lang]);
return;
} else {
$g_lang_strings[$p_lang] = array_replace( ((array)$g_lang_strings[$p_lang]), (array)$s_messages);
}
} else {
// new language loaded
$g_lang_strings[$p_lang] = $s_messages;
if( isset( $s_custom_messages[$p_lang] ) ) {
// 4.a - new string format + new custom string format
$g_lang_strings[$p_lang] = array_replace( ((array)$g_lang_strings[$p_lang]), (array)$s_custom_messages[$p_lang]);
return;
}
}
}

// 4.b new string format + old custom string format
// 4.c - old string format + old custom string format
if( !isset( $s_messages ) || file_exists( $t_custom_strings ) ) {
$t_vars = get_defined_vars();

Expand All @@ -109,6 +133,11 @@ function lang_load( $p_lang, $p_dir = null ) {
}
}
}
// 4.d old string format + new custom string format
// merge new custom strings into array in same way we merge in 4.a
if( isset( $s_custom_messages[$p_lang] ) ) {
$g_lang_strings[$p_lang] = array_replace( ((array)$g_lang_strings[$p_lang]), (array)$s_custom_messages[$p_lang]);
}
}
}

Expand Down Expand Up @@ -276,9 +305,10 @@ function lang_get_current() {
* 2. The string in English
* @param string $p_string
* @param string $p_lang
* @param bool $p_error default: true - error if string not found
* @return string
*/
function lang_get( $p_string, $p_lang = null ) {
function lang_get( $p_string, $p_lang = null, $p_error = true ) {
global $g_lang_strings;

# If no specific language is requested, we'll
Expand All @@ -294,31 +324,37 @@ function lang_get( $p_string, $p_lang = null ) {
// Now we'll make sure that the requested language is loaded
lang_ensure_loaded( $t_lang );

# note in the current implementation we always return the same value
# because we don't have a concept of falling back on a language. The
# language files actually *contain* English strings if none has been
# defined in the correct language
# @todo thraxisp - not sure if this is still true. Strings from last language loaded
# may still be in memeory if a new language is loaded.

// Step 1 - see if language string exists in requested language
if( lang_exists( $p_string, $t_lang ) ) {
return $g_lang_strings[$t_lang][$p_string];
} else {
// Language string doesn't exist in requested language

// Step 2 - See if language string exists in current plugin
$t_plugin_current = plugin_get_current();
if( !is_null( $t_plugin_current ) ) {
// Step 3 - Plugin exists: load language file
lang_load( $t_lang, config_get( 'plugin_path' ) . $t_plugin_current . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR );
if( lang_exists( $p_string, $t_lang ) ) {
return $g_lang_strings[$t_lang][$p_string];
}

// Step 4 - Localised language entry didn't exist - fallback to english for plugin
lang_load( 'english', config_get( 'plugin_path' ) . $t_plugin_current . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR );
if( lang_exists( $p_string, $t_lang ) ) {
return $g_lang_strings[$t_lang][$p_string];
}
}

// Step 5 - string didn't exist, try fall back to english:
if( $t_lang == 'english' ) {
error_parameters( $p_string );
trigger_error( ERROR_LANG_STRING_NOT_FOUND, WARNING );
if( $p_error ) {
error_parameters( $p_string );
trigger_error( ERROR_LANG_STRING_NOT_FOUND, WARNING );
}
return '';
} else {

# if string is not found in a language other than english, then retry using the english language.
// if string is not found in a language other than english, then retry using the english language.
return lang_get( $p_string, 'english' );
}
}
Expand Down
8 changes: 1 addition & 7 deletions core/plugin_api.php
Expand Up @@ -280,14 +280,8 @@ function plugin_error( $p_error_name, $p_error_type = ERROR, $p_basename = null
}

$t_error_code = "plugin_${t_basename}_${p_error_name}";
$MANTIS_ERROR = lang_get( 'MANTIS_ERROR' );

if( isset( $MANTIS_ERROR[$t_error_code] ) ) {
trigger_error( $t_error_code, $p_error_type );
} else {
error_parameters( $p_error_name, $t_basename );
trigger_error( ERROR_PLUGIN_GENERIC, ERROR );
}
trigger_error( $t_error_code, $p_error_type );

return null;
}
Expand Down

0 comments on commit 57a8ee8

Please sign in to comment.