MyBB Community Forums

Full Version: English as language string fallback
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I find it frustrating that if x string is not available in the language set by the user, then nothing is displayed. IMO the english language pack should always be loaded as a fallback for any string not found in secondary packs.
I think this can be somehow achieved, adding the following right at the beginning of MyLanguage::load() :
		if(file_exists($this->path."/".$this->fallback."/".$section.".lang.php"))
		{
			require_once $this->path."/".$this->fallback."/".$section.".lang.php";
		}
I agree, this would be a great improvement. The above patch would only work for language files though, wouldn't it? In an ideal world, it would work for individual language strings doing something like array_merge(english, native).
The code above loads the fallback language file before attempting to load the current language one, once the current language file is loaded the strings will be overwritten without using array_merge(). The code would look something like the following:
function load($section, $isdatahandler=false, $supress_error=false)
{
	// Assign language variables.
	// Datahandlers are never in admin lang directory.
	if($isdatahandler === true)
	{
		$lfile = $this->path."/".str_replace('/admin', '', $this->language)."/".$section.".lang.php";
		$fallback_file = $this->path."/".str_replace('/admin', '', $this->fallback)."/".$section.".lang.php";
	}
	else
	{
		$lfile = $this->path."/".$this->language."/".$section.".lang.php";
		$fallback_file = $this->path."/".$this->fallback."/".$section.".lang.php";
	}

	// Load the fallback language file before attempting to load the custom language file
	if(file_exists($fallback_file))
	{
		require_once $fallback_file;
	} 

	if(file_exists($lfile))
	{
		require_once $lfile;
	}
	elseif(!file_exists($fallback_file)) // Surpress error if fallback file was loaded
	{
		if($supress_error != true)
		{
			die("$lfile does not exist");
		}
	}

	// We must unite and protect our language variables!
	$lang_keys_ignore = array('language', 'path', 'settings');

	if(isset($l) && is_array($l))
	{
		foreach($l as $key => $val)
		{
			if((empty($this->$key) || $this->$key != $val) && !in_array($key, $lang_keys_ignore))
			{
				$this->$key = $val;
			}
		}
	}
}

Note that since the fallback language file is being loaded then the error should be suppressed if the custom language file is not found.
That sounds good to me. The only minor nitpick is the duplicate file_exists() call - filesystem operations can be slow, so would be better to just assign a variable like $fallback_loaded.