ソースを参照

autodetect typography via language

euromark 12 年 前
コミット
c67e779166
2 ファイル変更105 行追加56 行削除
  1. 55 35
      Test/Case/View/Helper/TypographyHelperTest.php
  2. 50 21
      View/Helper/TypographyHelper.php

+ 55 - 35
Test/Case/View/Helper/TypographyHelperTest.php

@@ -11,34 +11,35 @@ class TypographyHelperTest extends MyCakeTestCase {
 
 	public $Typography;
 
-/**
- * setUp method
- *
- * @return void
- */
+	/**
+	 * setUp method
+	 *
+	 * @return void
+	 */
 	public function setUp() {
 		parent::setUp();
-		Configure::write('Typography.locale', '');
+		Configure::delete('Typography.locale');
+		Configure::write('App.language', 'eng');
 
 		$this->Typography = new TypographyHelper(new View(null));
 	}
 
-/**
- * tearDown method
- *
- * @return void
- */
+	/**
+	 * tearDown method
+	 *
+	 * @return void
+	 */
 	public function tearDown() {
 		unset($this->Typography);
 
 		parent::tearDown();
 	}
 
-/**
- * testFormatCharacter method
- *
- * @return void
- */
+	/**
+	 * testFormatCharacter method
+	 *
+	 * @return void
+	 */
 	public function testFormatCharacter() {
 		$strs = array(
 			'"double quotes"' => '“double quotes”',
@@ -60,9 +61,6 @@ class TypographyHelperTest extends MyCakeTestCase {
 			$this->assertEquals($expected, $result);
 		}
 
-		//$this->tearDown();
-		//$this->setUp();
-
 		Configure::write('Typography.locale', 'low');
 		$strs = array(
 			'"double quotes"' 				=> '„double quotes‟',
@@ -74,29 +72,51 @@ class TypographyHelperTest extends MyCakeTestCase {
 			//echo pre($result);
 			$this->assertEquals($expected, $result);
 		}
+
+		Configure::write('Typography.locale', 'angle');
+		$strs = array(
+			'"double quotes"' 				=> '«double quotes»',
+			'"testing" in "theory" that is' => '«testing» in «theory» that is',
+			"Here's what I'm" 				=> 'Here›s what I›m',
+		);
+		foreach ($strs as $str => $expected) {
+			$result = $this->Typography->formatCharacters($str);
+			echo debug($result);
+			$this->assertEquals($expected, $result);
+		}
 	}
 
-/**
- * testAutoTypography method
- *
- * @return void
- */
+	/**
+	 * testAutoTypography method
+	 *
+	 * @return void
+	 */
 	public function testAutoTypography() {
-		$str = 'Some \'funny\' and "funky" test with a new
+		$str = 'Some \'funny\' and "funky" test';
 
-paragraph and a
-	new line tabbed in.';
+		$result = $this->Typography->autoTypography($str);
+		//echo pre($result);
+		$expected = '<p>Some &#8216;funny&#8217; and &#8220;funky&#8221; test</p>';
+		$this->assertEquals($expected, $result);
 
-		$res = $this->Typography->autoTypography($str);
-		//echo pre($res);
-		//debug($res);
+		Configure::write('App.language', 'deu');
+		$result = $this->Typography->autoTypography($str);
+		//echo pre($result);
+		$expected = '<p>Some &sbquo;funny&#8219; and &bdquo;funky&#8223; test</p>';
+		$this->assertEquals($expected, $result);
+
+		Configure::write('App.language', 'fra');
+		$result = $this->Typography->autoTypography($str);
+		//echo debug($result);
+		$expected = '<p>Some &lsaquo;funny&rsaquo; and &#171;funky&#187; test</p>';
+		$this->assertEquals($expected, $result);
 	}
 
-/**
- * testNl2brExceptPre method
- *
- * @return void
- */
+	/**
+	 * testNl2brExceptPre method
+	 *
+	 * @return void
+	 */
 	public function testNl2brExceptPre() {
 		$str = <<<EOH
 Hello, I'm a happy string with some new lines.

+ 50 - 21
View/Helper/TypographyHelper.php

@@ -17,6 +17,12 @@ App::uses('AppHelper', 'View/Helper');
 /**
  * Typography Class converted to Cake Helper
  *
+ * In the database you usually got ", ', and - as uniform chars.
+ * On output you might want to localize them according to your country's/languages' preferences.
+ *
+ * For Swiss, for example: "Some quote" might become «Some quote»
+ * For German: "Some quote" might become „Some quote“
+ *
  * @modified Mark Scherer
  * @cakephp 2.x
  * @php 5
@@ -43,22 +49,32 @@ class TypographyHelper extends AppHelper {
 	// whether or not to protect quotes within { curly braces }
 	public $protect_braced_quotes = false;
 
+	public $matching = array(
+		'deu' => 'low', // except for Switzerland
+		'eng' => 'default',
+		'fra' => 'angle',
+	);
+
 	/**
-	 * Auto Typography
+	 * Automatically uses the typography specified.
+	 * By default, uses Configure::read('App.language') to determine locale preference.
+	 * It will then try to match the language to the type of characters used.
+	 * You can hardwire this by using Configure::read('Typography.locale'); and directly set it
+	 * to 'low' or 'angle'. It will then disregard the language.
 	 *
 	 * This function converts text, making it typographically correct:
-	 *	- Converts double spaces into paragraphs.
-	 *	- Converts single line breaks into <br /> tags
-	 *	- Converts single and double quotes into correctly facing curly quote entities.
-	 *	- Converts three dots into ellipsis.
-	 *	- Converts double dashes into em-dashes.
+	 * - Converts double spaces into paragraphs.
+	 * - Converts single line breaks into <br /> tags
+	 * - Converts single and double quotes into correctly facing curly quote entities.
+	 * - Converts three dots into ellipsis.
+	 * - Converts double dashes into em-dashes.
 	 * - Converts two spaces into entities
 	 *
-	 * @param string
-	 * @param boolean Whether to reduce more then two consecutive newlines to two
-	 * @return string
+	 * @param string $str Text
+	 * @param boolean $reduceLinebreaks Whether to reduce more then two consecutive newlines to two
+	 * @return string Text
 	 */
-	public function autoTypography($str, $reduce_linebreaks = false) {
+	public function autoTypography($str, $reduceLinebreaks = false) {
 		if ($str === '') {
 			return '';
 		}
@@ -70,7 +86,7 @@ class TypographyHelper extends AppHelper {
 
 		// Reduce line breaks. If there are more than two consecutive linebreaks
 		// we'll compress them down to a maximum of two since there's no benefit to more.
-		if ($reduce_linebreaks === true) {
+		if ($reduceLinebreaks === true) {
 			$str = preg_replace("/\n\n+/", "\n\n", $str);
 		}
 
@@ -205,7 +221,7 @@ class TypographyHelper extends AppHelper {
 			);
 
 		// Do we need to reduce empty lines?
-		if ($reduce_linebreaks === true) {
+		if ($reduceLinebreaks === true) {
 			$table['#<p>\n*</p>#'] = '';
 		} else {
 			// If we have empty paragraph tags we add a non-breaking space
@@ -232,20 +248,33 @@ class TypographyHelper extends AppHelper {
 		if ($locale === null) {
 			$locale = Configure::read('Typography.locale');
 		}
+		if (!$locale) {
+			$locale = 'default';
+			$language = Configure::read('App.language');
+			if ($language && isset($this->matching[$language])) {
+				$locale = $this->matching[$language];
+			}
+		}
 
 		$locales = array(
 			'default' => array(
-				'leftSingle' => '&#8216;', # &lsquo;
-				'rightSingle' => '&#8217;', # &rsquo;
-				'leftDouble' => '&#8220;', # &ldquo;
-				'rightDouble' => '&#8221;', # &rdquo;
+				'leftSingle' => '&#8216;', // &lsquo; / ‘
+				'rightSingle' => '&#8217;', // &rsquo; / ’
+				'leftDouble' => '&#8220;', // &ldquo; / “
+				'rightDouble' => '&#8221;', // &rdquo; / ”
 			),
 			'low' => array(
-				'leftSingle' => '&sbquo;',
-				'rightSingle' => '&#8219;',
-				'leftDouble' => '&bdquo;',
-				'rightDouble' => '&#8223;',
-			)
+				'leftSingle' => '&sbquo;', // &sbquo; / ‚
+				'rightSingle' => '&#8219;', // &rsquo; / ’
+				'leftDouble' => '&bdquo;', // &bdquo; / „
+				'rightDouble' => '&#8223;', // &rdquo; / ”
+			),
+			'angle' => array(
+				'leftSingle' => '&lsaquo;', // ‹
+				'rightSingle' => '&rsaquo;', // ›
+				'leftDouble' => '&#171;', // &laquo; / «
+				'rightDouble' => '&#187;', // &raquo; / »
+			),
 		);
 
 		if (!isset($table)) {