Merge pull request #8476 from nextcloud/strict_l10n

Make OCP\IL10N strict
This commit is contained in:
Roeland Jago Douma 2018-02-22 10:03:18 +01:00 committed by GitHub
commit c2c2c06546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 30 deletions

View File

@ -31,7 +31,7 @@ script('core', [
<p><?php p($l->t('Your data directory and files are probably accessible from the internet because the .htaccess file does not work.'));?><br> <p><?php p($l->t('Your data directory and files are probably accessible from the internet because the .htaccess file does not work.'));?><br>
<?php print_unescaped($l->t( <?php print_unescaped($l->t(
'For information how to properly configure your server, please see the <a href="%s" target="_blank" rel="noreferrer noopener">documentation</a>.', 'For information how to properly configure your server, please see the <a href="%s" target="_blank" rel="noreferrer noopener">documentation</a>.',
link_to_docs('admin-install') [link_to_docs('admin-install')]
)); ?></p> )); ?></p>
</fieldset> </fieldset>
<?php endif; ?> <?php endif; ?>

View File

@ -140,19 +140,19 @@ class DependencyAnalyzer {
if (isset($dependencies['php']['@attributes']['min-version'])) { if (isset($dependencies['php']['@attributes']['min-version'])) {
$minVersion = $dependencies['php']['@attributes']['min-version']; $minVersion = $dependencies['php']['@attributes']['min-version'];
if ($this->compareSmaller($this->platform->getPhpVersion(), $minVersion)) { if ($this->compareSmaller($this->platform->getPhpVersion(), $minVersion)) {
$missing[] = (string)$this->l->t('PHP %s or higher is required.', $minVersion); $missing[] = (string)$this->l->t('PHP %s or higher is required.', [$minVersion]);
} }
} }
if (isset($dependencies['php']['@attributes']['max-version'])) { if (isset($dependencies['php']['@attributes']['max-version'])) {
$maxVersion = $dependencies['php']['@attributes']['max-version']; $maxVersion = $dependencies['php']['@attributes']['max-version'];
if ($this->compareBigger($this->platform->getPhpVersion(), $maxVersion)) { if ($this->compareBigger($this->platform->getPhpVersion(), $maxVersion)) {
$missing[] = (string)$this->l->t('PHP with a version lower than %s is required.', $maxVersion); $missing[] = (string)$this->l->t('PHP with a version lower than %s is required.', [$maxVersion]);
} }
} }
if (isset($dependencies['php']['@attributes']['min-int-size'])) { if (isset($dependencies['php']['@attributes']['min-int-size'])) {
$intSize = $dependencies['php']['@attributes']['min-int-size']; $intSize = $dependencies['php']['@attributes']['min-int-size'];
if ($intSize > $this->platform->getIntSize()*8) { if ($intSize > $this->platform->getIntSize()*8) {
$missing[] = (string)$this->l->t('%sbit or higher PHP required.', $intSize); $missing[] = (string)$this->l->t('%sbit or higher PHP required.', [$intSize]);
} }
} }
return $missing; return $missing;
@ -209,7 +209,7 @@ class DependencyAnalyzer {
} }
$commandName = $this->getValue($command); $commandName = $this->getValue($command);
if (!$this->platform->isCommandKnown($commandName)) { if (!$this->platform->isCommandKnown($commandName)) {
$missing[] = (string)$this->l->t('The command line tool %s could not be found', $commandName); $missing[] = (string)$this->l->t('The command line tool %s could not be found', [$commandName]);
} }
} }
return $missing; return $missing;
@ -236,7 +236,7 @@ class DependencyAnalyzer {
$libName = $this->getValue($lib); $libName = $this->getValue($lib);
$libVersion = $this->platform->getLibraryVersion($libName); $libVersion = $this->platform->getLibraryVersion($libName);
if (is_null($libVersion)) { if (is_null($libVersion)) {
$missing[] = (string)$this->l->t('The library %s is not available.', $libName); $missing[] = (string)$this->l->t('The library %s is not available.', [$libName]);
continue; continue;
} }

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* *
@ -60,7 +61,6 @@ class L10N implements IL10N {
$this->app = $app; $this->app = $app;
$this->lang = $lang; $this->lang = $lang;
$this->translations = [];
foreach ($files as $languageFile) { foreach ($files as $languageFile) {
$this->load($languageFile); $this->load($languageFile);
} }
@ -71,7 +71,7 @@ class L10N implements IL10N {
* *
* @return string language * @return string language
*/ */
public function getLanguageCode() { public function getLanguageCode(): string {
return $this->lang; return $this->lang;
} }
@ -84,7 +84,7 @@ class L10N implements IL10N {
* Returns the translation. If no translation is found, $text will be * Returns the translation. If no translation is found, $text will be
* returned. * returned.
*/ */
public function t($text, $parameters = array()) { public function t(string $text, array $parameters = []): string {
return (string) new L10NString($this, $text, $parameters); return (string) new L10NString($this, $text, $parameters);
} }
@ -103,18 +103,18 @@ class L10N implements IL10N {
* provided by the po file. * provided by the po file.
* *
*/ */
public function n($text_singular, $text_plural, $count, $parameters = array()) { public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string {
$identifier = "_${text_singular}_::_${text_plural}_"; $identifier = "_${text_singular}_::_${text_plural}_";
if (isset($this->translations[$identifier])) { if (isset($this->translations[$identifier])) {
return (string) new L10NString($this, $identifier, $parameters, $count); return (string) new L10NString($this, $identifier, $parameters, $count);
} else { }
if ($count === 1) { if ($count === 1) {
return (string) new L10NString($this, $text_singular, $parameters, $count); return (string) new L10NString($this, $text_singular, $parameters, $count);
} else { }
return (string) new L10NString($this, $text_plural, $parameters, $count); return (string) new L10NString($this, $text_plural, $parameters, $count);
} }
}
}
/** /**
* Localization * Localization
@ -138,7 +138,7 @@ class L10N implements IL10N {
* - firstday: Returns the first day of the week (0 sunday - 6 saturday) * - firstday: Returns the first day of the week (0 sunday - 6 saturday)
* - jsdate: Returns the short JS date format * - jsdate: Returns the short JS date format
*/ */
public function l($type, $data = null, $options = array()) { public function l(string $type, $data = null, array $options = []) {
// Use the language of the instance // Use the language of the instance
$locale = $this->getLanguageCode(); $locale = $this->getLanguageCode();
if ($locale === 'sr@latin') { if ($locale === 'sr@latin') {
@ -155,14 +155,15 @@ class L10N implements IL10N {
$value = new \DateTime(); $value = new \DateTime();
if ($data instanceof \DateTime) { if ($data instanceof \DateTime) {
$value = $data; $value = $data;
} else if (is_string($data) && !is_numeric($data)) { } else if (\is_string($data) && !is_numeric($data)) {
$data = strtotime($data); $data = strtotime($data);
$value->setTimestamp($data); $value->setTimestamp($data);
} else if ($data !== null) { } else if ($data !== null) {
$data = (int)$data;
$value->setTimestamp($data); $value->setTimestamp($data);
} }
$options = array_merge(array('width' => 'long'), $options); $options = array_merge(['width' => 'long'], $options);
$width = $options['width']; $width = $options['width'];
switch ($type) { switch ($type) {
case 'date': case 'date':
@ -184,7 +185,7 @@ class L10N implements IL10N {
* Called by \OC_L10N_String * Called by \OC_L10N_String
* @return array * @return array
*/ */
public function getTranslations() { public function getTranslations(): array {
return $this->translations; return $this->translations;
} }
@ -192,10 +193,10 @@ class L10N implements IL10N {
* Returnsed function accepts the argument $n * Returnsed function accepts the argument $n
* *
* Called by \OC_L10N_String * Called by \OC_L10N_String
* @return string the plural form function * @return \Closure the plural form function
*/ */
public function getPluralFormFunction() { public function getPluralFormFunction(): \Closure {
if (is_null($this->pluralFormFunction)) { if (\is_null($this->pluralFormFunction)) {
$lang = $this->getLanguageCode(); $lang = $this->getLanguageCode();
$this->pluralFormFunction = function($n) use ($lang) { $this->pluralFormFunction = function($n) use ($lang) {
return PluralizationRules::get($n, $lang); return PluralizationRules::get($n, $lang);
@ -206,12 +207,12 @@ class L10N implements IL10N {
} }
/** /**
* @param $translationFile * @param string $translationFile
* @return bool * @return bool
*/ */
protected function load($translationFile) { protected function load(string $translationFile): bool {
$json = json_decode(file_get_contents($translationFile), true); $json = json_decode(file_get_contents($translationFile), true);
if (!is_array($json)) { if (!\is_array($json)) {
$jsonError = json_last_error(); $jsonError = json_last_error();
\OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']); \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
return false; return false;

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* *
@ -51,7 +52,7 @@ interface IL10N {
* returned. * returned.
* @since 6.0.0 * @since 6.0.0
*/ */
public function t($text, $parameters = array()); public function t(string $text, array $parameters = []): string;
/** /**
* Translating * Translating
@ -69,7 +70,7 @@ interface IL10N {
* @since 6.0.0 * @since 6.0.0
* *
*/ */
public function n($text_singular, $text_plural, $count, $parameters = array()); public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string;
/** /**
* Localization * Localization
@ -96,7 +97,7 @@ interface IL10N {
* - params: timestamp (int/string) * - params: timestamp (int/string)
* @since 6.0.0 - parameter $options was added in 8.0.0 * @since 6.0.0 - parameter $options was added in 8.0.0
*/ */
public function l($type, $data, $options = array()); public function l(string $type, $data, array $options = []);
/** /**
@ -105,5 +106,5 @@ interface IL10N {
* @return string language * @return string language
* @since 7.0.0 * @since 7.0.0
*/ */
public function getLanguageCode(); public function getLanguageCode(): string ;
} }