Permalink
Browse files

german translation

  • Loading branch information...
1 parent a976663 commit 6f48ec46b429d039c79c58b2c3bc421cb90b602f @cweiske cweiske committed May 9, 2012
View
@@ -50,6 +50,16 @@ class Date_HumanDiff
protected $formats;
/**
+ * Translation object.
+ * Provides translations for time strings
+ *
+ * @var Date_HumanDiff_Lang
+ */
+ protected $translator;
+
+
+
+ /**
* Create new instance, initialize $formats array
*/
public function __construct()
@@ -93,7 +103,10 @@ public function get($timestamp, $reference = null)
foreach ($this->formats as $format) {
if ($delta < $format[0]) {
- return sprintf($format[1], round($delta / $format[2]));
+ return sprintf(
+ $this->getTranslation($format[1]),
+ round($delta / $format[2])
+ );
}
};
}
@@ -120,6 +133,92 @@ protected function makeTimestamp($something)
return strtotime($something);
}
+
+ /**
+ * Get the translation for the given string.
+ *
+ * @param string $string String to translate
+ *
+ * @return string Translated string. Original string when no translation
+ * exists.
+ */
+ protected function getTranslation($string)
+ {
+ if ($this->translator === null) {
+ return $string;
+ }
+
+ return $this->translator->get($string);
+ }
+
+ /**
+ * Set the object that's used to translate time strings
+ *
+ * @param object $translator Language translation object
+ *
+ * @return void
+ */
+ public function setTranslator(Date_HumanDiff_Lang $translator)
+ {
+ $this->translator = $translator;
+ }
+
+ /**
+ * Set the language to use.
+ *
+ * Supported formats:
+ * - 2-letter ISO code ("de", "fr")
+ * - locale name with and without encoding ("de_AT", "fr_FR.utf8")
+ *
+ * @param string $lang Language name
+ *
+ * @return boolean True if the translations could be loaded, false if not.
+ */
+ public function setLanguage($lang)
+ {
+ if (strlen($lang) > 2) {
+ //split off encoding
+ list($locale,) = explode('.', $lang);
+ if (!preg_match('#^[a-z_]+$#i', $locale)) {
+ return false;
+ }
+ $lang = $locale;
+ }
+
+ $class = 'Date_HumanDiff_Lang_' . $lang;
+ $file = str_replace('_', '/', $class) . '.php';
+ if ($this->isIncludable($file)) {
+ include_once $file;
+ }
+ if (!class_exists($class)) {
+ if (strlen($lang) > 2) {
+ //try main language without country
+ return $this->setLanguage(substr($lang, 0, 2));
+ }
+ return false;
+ }
+
+ $translator = new $class();
+ $this->setTranslator($translator);
+ return true;
+ }
+
+ /**
+ * Check if the given file is includable
+ *
+ * @param string $file Path to file (relative to include path)
+ *
+ * @return boolean True if one can include() it
+ */
+ protected function isIncludable($file)
+ {
+ $hdl = @fopen($file, 'r', true);
+ if ($hdl === false) {
+ return false;
+ }
+ fclose($hdl);
+ return true;
+ }
}
?>
@@ -0,0 +1,15 @@
+<?php
+
+interface Date_HumanDiff_Lang
+{
+ /**
+ * Get the translation for the given string.
+ *
+ * @param string $string String to translate
+ *
+ * @return string Translated string
+ */
+ public function get($string);
+}
+
+?>
@@ -0,0 +1,28 @@
+<?php
+require_once 'Date/HumanDiff/LangArray.php';
+
+class Date_HumanDiff_Lang_de extends Date_HumanDiff_LangArray
+{
+ /**
+ * Translation array.
+ * Key is the english variant, value the german translation.
+ *
+ * @var array
+ */
+ public $trans = array(
+ 'just now' => 'gerade eben',
+ 'a minute ago' => 'vor einer Minute',
+ '%d minutes ago' => 'vor %d Minuten',
+ 'an hour ago' => 'vor einer Stunde',
+ '%d hours ago' => 'vor %d Stunden',
+ 'yesterday' => 'gestern',
+ '%d days ago' => 'vor %d Tagen',
+ 'a week ago' => 'vor einer Woche',
+ '%d weeks ago' => 'vor %d Wochen',
+ 'a month ago' => 'vor einem Monat',
+ '%d months ago' => 'vor %d Monaten',
+ 'a year ago' => 'vor einem Jahr',
+ '%d years ago' => 'vor %d Jahren',
+ );
+}
+?>
@@ -0,0 +1,23 @@
+<?php
+require_once 'Date/HumanDiff/Lang.php';
+
+abstract class Date_HumanDiff_LangArray implements Date_HumanDiff_Lang
+{
+ /**
+ * Get the translation for the given string.
+ *
+ * @param string $string String to translate
+ *
+ * @return string Translated string. Original string when no translation
+ * exists.
+ */
+ public function get($string)
+ {
+ if (!isset($this->trans[$string])) {
+ return $string;
+ }
+ return $this->trans[$string];
+ }
+}
+
+?>
@@ -96,6 +96,30 @@ public function testGetStringDateTime()
)
);
}
+
+ public function testSetLanguageExisting()
+ {
+ $this->assertTrue($this->dh->setLanguage('de'));
+ $this->assertEquals('gerade eben', $this->dh->get(time()));
+ }
+
+ public function testSetLanguageParentExisting()
+ {
+ $this->assertTrue($this->dh->setLanguage('de_AT'));
+ $this->assertEquals('gerade eben', $this->dh->get(time()));
+ }
+
+ public function testSetLanguageNotExisting()
+ {
+ $this->assertFalse($this->dh->setLanguage('mg'));
+ $this->assertEquals('just now', $this->dh->get(time()));
+ }
+
+ public function testSetLanguageInvalid()
+ {
+ $this->assertFalse($this->dh->setLanguage('/path/to/some/file.php'));
+ $this->assertEquals('just now', $this->dh->get(time()));
+ }
}
?>

0 comments on commit 6f48ec4

Please sign in to comment.