/** * Generates a URI path segment for a given node taking it's language dimension into account * * @param NodeInterface $node Optional node to determine language dimension * @param string $text Optional text * @return string */ public function generateUriPathSegment(NodeInterface $node = null, $text = null) { if ($node) { $text = $text ?: $node->getLabel() ?: $node->getName(); $dimensions = $node->getContext()->getDimensions(); if (array_key_exists('language', $dimensions) && $dimensions['language'] !== array()) { $locale = new Locale($dimensions['language'][0]); $language = $locale->getLanguage(); } } elseif (strlen($text) === 0) { throw new Exception('Given text was empty.', 1457591815); } $text = $this->transliterationService->transliterate($text, isset($language) ? $language : null); return Transliterator::urlize($text); }
/** * Returns translated label ("target" tag in XLIFF) for the id given. * Id is compared with "id" attribute of "trans-unit" tag (see XLIFF * specification for details). * * @param string $transUnitId The "id" attribute of "trans-unit" tag in XLIFF * @param integer $pluralFormIndex Index of plural form to use (starts with 0) * @return mixed Translated label or FALSE on failure */ public function getTargetByTransUnitId($transUnitId, $pluralFormIndex = 0) { if (!isset($this->xmlParsedData['translationUnits'][$transUnitId])) { $this->i18nLogger->log('No trans-unit element with the id "' . $transUnitId . '" was found in ' . $this->sourcePath . '. Either this translation has been removed or the id in the code or template referring to the translation is wrong.', LOG_DEBUG); return false; } if (!isset($this->xmlParsedData['translationUnits'][$transUnitId][$pluralFormIndex])) { $this->i18nLogger->log('The plural form index "' . $pluralFormIndex . '" for the trans-unit element with the id "' . $transUnitId . '" in ' . $this->sourcePath . ' is not available.', LOG_DEBUG); return false; } if ($this->xmlParsedData['translationUnits'][$transUnitId][$pluralFormIndex]['target']) { return $this->xmlParsedData['translationUnits'][$transUnitId][$pluralFormIndex]['target']; } elseif ($this->locale->getLanguage() === $this->xmlParsedData['sourceLocale']->getLanguage()) { return $this->xmlParsedData['translationUnits'][$transUnitId][$pluralFormIndex]['source'] ?: false; } else { $this->i18nLogger->log('The target translation was empty and the source translation language (' . $this->xmlParsedData['sourceLocale']->getLanguage() . ') does not match the current locale (' . $this->locale->getLanguage() . ') for the trans-unit element with the id "' . $transUnitId . '" in ' . $this->sourcePath, LOG_DEBUG); return false; } }
/** * @test */ public function theConstructorRecognizesTheMostImportantValidLocaleIdentifiers() { $locale = new I18n\Locale('de'); $this->assertEquals('de', $locale->getLanguage()); $this->assertNull($locale->getScript()); $this->assertNull($locale->getRegion()); $this->assertNull($locale->getVariant()); $locale = new I18n\Locale('de_DE'); $this->assertEquals('de', $locale->getLanguage()); $this->assertEquals('DE', $locale->getRegion()); $this->assertNull($locale->getScript()); $this->assertNull($locale->getVariant()); $locale = new I18n\Locale('en_Latn_US'); $this->assertEquals('en', $locale->getLanguage()); $this->assertEquals('Latn', $locale->getScript()); $this->assertEquals('US', $locale->getRegion()); $this->assertNull($locale->getVariant()); $locale = new I18n\Locale('AR-arab_ae'); $this->assertEquals('ar', $locale->getLanguage()); $this->assertEquals('Arab', $locale->getScript()); $this->assertEquals('AE', $locale->getRegion()); $this->assertNull($locale->getVariant()); }
/** * Returns array of plural forms available for particular locale. * * @param Locale $locale Locale to return plural forms for * @return array Plural forms' names (one, zero, two, few, many, other) available for language set in this model */ public function getPluralForms(Locale $locale) { if (!isset($this->rulesetsIndices[$locale->getLanguage()])) { return [self::RULE_OTHER]; } return array_merge(array_keys($this->rulesets[$locale->getLanguage()][$this->rulesetsIndices[$locale->getLanguage()]]), [self::RULE_OTHER]); }