Example #1
0
 /**
  * Get the correct form of a word
  *
  * @param  string $word     The word to fetch
  * @param  string $patterns A particular patterns array to fetch from
  * @return string           Correct form of the word
  */
 protected static function getWord($word, $patterns = 'patterns')
 {
     // If the world is invariable, don't touch it
     if (static::invariable($word)) {
         return $word;
     }
     // If it's irregular, return the correct form
     if (!is_null(static::irregular($word))) {
         return static::irregular($word);
     }
     // Else look for patterns ans replace
     foreach (Babel::translate('accord/' . static::$repository . '.' . $patterns) as $pattern => $replace) {
         if (preg_match($pattern, $word)) {
             return preg_replace($pattern, $replace, $word);
         }
     }
     // Special case for english language to use Laravel's plural function
     if (Babel::lang() == 'en' and static::$repository == 'plurals') {
         return Str::plural($word);
     }
     return $word;
 }
Example #2
0
 public function testFormUpdate()
 {
     $babel = Babel::form('update', 'category', 'foo');
     $this->assertEquals('Modifier la catégorie « foo »', $babel);
 }
 public function testResults()
 {
     $message = Babel::create()->number(4)->noun('result');
     $this->assertEquals('4 results', $message->speak());
 }
Example #4
0
 /**
  * Add a verb to the sentence
  *
  * @param  string $verb A verb
  */
 public function verb($verb)
 {
     // If we passed a controller's action
     if (str_contains($verb, '_')) {
         $verb = array_get(explode('_', $verb), 1);
     }
     $verb = Babel::verb($verb);
     $this->addWord('verb', $verb);
     return $this;
 }
Example #5
0
 /**
  * Checks if a noun is male or female
  *
  * @param  string  $noun A noun
  * @return boolean
  */
 public static function isFemale($noun)
 {
     $noun = Str::singular($noun);
     $female = Babel::getTranslations('accord/genders', 'female');
     return in_array($noun, $female);
 }
Example #6
0
 public function testIrregular()
 {
     $babel = Babel::create()->article('the')->noun('document')->verb('submit')->speak();
     $this->assertEquals('Le document a été soumis', $babel);
 }
Example #7
0
 /**
  * Automatic transation and verb setting
  *
  * @param  string $method     The kind of word to translate
  * @param  string $parameters The key to get
  * @return string             Translated word
  */
 public static function __callStatic($method, $parameters)
 {
     // Check for an extended sentence
     if (isset(static::$extend[$method])) {
         return call_user_func_array(static::$extend[$method], $parameters);
     }
     // Get a custom translation
     if (in_array($method, array('adjective', 'article', 'bit', 'noun', 'number', 'plural', 'state', 'verb'))) {
         $word = array_get($parameters, 0);
         if (is_null($word)) {
             return false;
         }
         return Babel::translate($method . 's.' . $word, $word);
     }
     // Verb setting
     $parameters[] = rtrim($method, 'd');
     return call_user_func_array('static::many', $parameters);
 }
Example #8
0
 /**
  * Accord an adjective
  *
  * @param  string $adjective An adjective
  * @return string            An accorded adjective
  */
 public static function adjective($adjective)
 {
     $message = Message::current();
     switch (Babel::lang()) {
         case 'fr':
             if ($message->isFemale()) {
                 $adjective .= 'e';
             }
             if (Word::isPlural($message->noun) or $message->number > 1) {
                 $adjective .= 's';
             }
     }
     return $adjective;
 }
Example #9
0
 public function testAccordWoman()
 {
     $babel = Babel::create()->article('a')->noun('category');
     $this->assertEquals("Une catégorie", $babel->speak());
 }
Example #10
0
 public function testAccordArticle()
 {
     $babel = Babel::create()->article('a')->noun('apricot');
     $this->assertEquals('An apricot', $babel->speak());
 }
Example #11
0
 public function testFormUpdate()
 {
     $babel = Babel::form('update', 'category', 'foo');
     $this->assertEquals('Update the category « foo »', $babel);
 }