Przeglądaj źródła

! Further tweaks to spell checking to account for enchant being UTF-8 only (see comments on #

Signed-off-by: Michael Eshom <[email protected]>
Michael Eshom 10 lat temu
rodzic
commit
f4a33bd3c6
3 zmienionych plików z 47 dodań i 10 usunięć
  1. 9 2
      Sources/ManagePosts.php
  2. 1 1
      Sources/Subs-Editor.php
  3. 37 7
      Sources/Subs-Post.php

+ 9 - 2
Sources/ManagePosts.php

@@ -188,13 +188,20 @@ function ModifyPostSettings($return_config = false)
 {
 	global $context, $txt, $modSettings, $scripturl, $sourcedir, $smcFunc, $db_prefix, $db_type;
 
+	// Make an inline conditional a little shorter...
+	$can_spell_check = false;
+	if (function_exists('pspell_new'))
+		$can_spell_check = true;
+	elseif (function_exists('enchant_broker_init') && ($txt['lang_charset'] == 'UTF-8' || function_exists('iconv')))
+		$can_spell_check = true;			
+
 	// All the settings...
 	$config_vars = array(
 			// Simple post options...
 			array('check', 'removeNestedQuotes'),
 			array('check', 'enableEmbeddedFlash', 'subtext' => $txt['enableEmbeddedFlash_warning']),
-			// Note show the warning as read if pspell not installed!
-			array('check', 'enableSpellChecking', 'subtext' => ((function_exists('pspell_new') || function_exists('enchant_broker_init')) ? $txt['enableSpellChecking_warning'] : ('<span class="alert">' . $txt['enableSpellChecking_warning'] . '</span>'))),
+			// Note show the warning as red if: pspell not installed and (enchant not installed or not using UTF-8 and iconv not installed) 
+			array('check', 'enableSpellChecking', 'subtext' => ($can_spell_check ? $txt['enableSpellChecking_warning'] : ('<span class="alert">' . $txt['enableSpellChecking_warning'] . '</span>'))),
 			array('check', 'disable_wysiwyg'),
 		'',
 			// Posting limits...

+ 1 - 1
Sources/Subs-Editor.php

@@ -1489,7 +1489,7 @@ function create_control_richedit($editorOptions)
 			loadJavascriptFile($scripturl . '?action=loadeditorlocale', array(), 'sceditor_language');
 
 		$context['shortcuts_text'] = $txt['shortcuts' . (!empty($context['drafts_save']) ? '_drafts' : '') . (isBrowser('is_firefox') ? '_firefox' : '')];
-		$context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && (function_exists('pspell_new') || function_exists('enchant_broker_init'));
+		$context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && (function_exists('pspell_new') || (function_exists('enchant_broker_init') && ($txt['lang_charset'] == 'UTF-8' || function_exists('iconv'))));
 		if ($context['show_spellchecking'])
 		{
 			loadJavascriptFile('spellcheck.js', array('default_theme' => true));

+ 37 - 7
Sources/Subs-Post.php

@@ -2976,16 +2976,21 @@ function spell_init()
 {
 	global $context, $txt;
 
+	// Check for UTF-8 and strip ".utf8" off the lang_locale string for enchant
+	$context['spell_utf8'] = ($txt['lang_character_set'] == 'UTF-8');
+	$lang_locale = str_replace('.utf8', '', $txt['lang_locale']);
+
 	// Try enchant first since PSpell is (supposedly) deprecated as of PHP 5.3
-	if (function_exists('enchant_broker_init'))
+	// enchant only does UTF-8, so we need iconv if you aren't using UTF-8
+	if (function_exists('enchant_broker_init') && ($context['spell_utf8'] || function_exists('iconv')))
 	{
 		// We'll need this to free resources later...
 		$context['enchant_broker'] = enchant_broker_init();
 
 		// Try locale first, then general...
-		if (!empty($txt['lang_locale']) && enchant_broker_dict_exists($context['enchant_broker'], $txt['lang_locale']))
-		{
-			$enchant_link = enchant_broker_request_dict($context['enchant_broker'], $txt['lang_locale']);
+		if (!empty($lang_locale) && enchant_broker_dict_exists($context['enchant_broker'], $lang_locale))
+		{			
+			$enchant_link = enchant_broker_request_dict($context['enchant_broker'], $lang_locale);
 		}
 		elseif (enchant_broker_dict_exists($context['enchant_broker'], $txt['lang_dictionary']))
 		{
@@ -3048,11 +3053,17 @@ function spell_init()
  */
 function spell_check($dict, $word)
 {
-	global $context;
+	global $context, $txt;
 
 	// Enchant or pspell?
 	if ($context['provider'] == 'enchant')
 	{
+		// This is a bit tricky here...
+		if (!$context['spell_utf8'])
+		{
+			// Convert the word to UTF-8 with iconv
+			$word = iconv($txt['lang_charset'], 'UTF-8', $word);
+		}
 		return enchant_dict_check($dict, $word);
 	}
 	elseif ($context['provider'] == 'pspell')
@@ -3072,11 +3083,30 @@ function spell_check($dict, $word)
  */
 function spell_suggest($dict, $word)
 {
-	global $context;
+	global $context, $txt;
 
 	if ($context['provider'] == 'enchant')
 	{
-		return enchant_dict_suggest($dict, $word);
+		// If we're not using UTF-8, we need iconv to handle some stuff...
+		if (!$context['spell_utf8'])
+		{
+			// Convert the word to UTF-8 before getting suggestions
+			$word = iconv($txt['lang_charset'], 'UTF-8', $word);
+			$suggestions = enchant_dict_suggest($dict, $word);
+			
+			// Go through the suggestions and convert them back to the proper character set
+			foreach($suggestions as $index => $suggestion)
+			{
+				// //TRANSLIT makes it use similar-looking characters for incompatible ones...
+				$suggestions[$index] = iconv('UTF-8', $txt['lang_charset'] . '//TRANSLIT', $suggestion);
+			}
+
+			return $suggestions;
+		}
+		else
+		{
+			return enchant_dict_suggest($dict, $word);
+		}
 	}
 	elseif ($context['provider'] == 'pspell')
 	{