Skip to content

Commit

Permalink
feat: make gettext features optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nelson6e65 committed Feb 19, 2024
1 parent 2fc2d6b commit 8fb2ec5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
21 changes: 11 additions & 10 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
* - File to perform manual autoload. For non composer instalation, must be
* required at app initialization.
*
* Copyright © 2015-2021 Nelson Martell (http://nelson6e65.github.io)
* Copyright © 2015-2024 Nelson Martell (http://nelson6e65.github.io)
*
* Licensed under The MIT License (MIT)
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright 2015-2021 Nelson Martell
* @copyright 2015-2024 Nelson Martell
* @link http://nelson6e65.github.io/php_nml/
* @since 0.3.0
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
Expand Down Expand Up @@ -67,17 +67,18 @@ function autoloadNelsonMartellLibrary($class)
} elseif (is_file($path . '.inc')) {
$path .= '.inc';
} else {
$msg = 'Unable to auto-load "%s" class in Nelson Martell Library (NML): "%s" file was not found.' .
' You can see the API documentation (http://nelson6e65.github.io/php_nml/api) in order to check ' .
' availability of all classes/namespaces in NML. Note: If you are using "NelsonMartell" as main namespace' .
' in a file that not belongs to NML, you should include it before to load "NML/autoload.php" or,' .
' using SPL autoload features, register autoload function for that class(es) using "prepend" argument for' .
' spl_autoload_register function set to TRUE.';
$msg =
'Unable to auto-load "%s" class in Nelson Martell Library (NML): "%s" file was not found.' .
' You can see the API documentation (http://nelson6e65.github.io/php_nml/api) in order to check ' .
' availability of all classes/namespaces in NML. Note: If you are using "NelsonMartell" as main namespace' .
' in a file that not belongs to NML, you should include it before to load "NML/autoload.php" or,' .
' using SPL autoload features, register autoload function for that class(es) using "prepend" argument for' .
' spl_autoload_register function set to TRUE.';

throw new Exception(sprintf(dgettext('nml', $msg), $class, $path));
throw new Exception(sprintf(NelsonMartell\msg($msg), $class, $path));
}

require_once($path);
require_once $path;
}

spl_autoload_register('autoloadNelsonMartellLibrary');
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
"docs": "http://nelson6e65.github.io/php_nml/api"
},
"require": {
"php": ">=7.2"
"php": ">=7.2",
"ext-mbstring": ">=7.2",
"ext-json": "*"
},
"suggest": {
"ext-gettext": "Required to enable translations and some functions"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.0",
Expand Down
6 changes: 5 additions & 1 deletion config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright 2015-2021 Nelson Martell
* @copyright 2015-2024 Nelson Martell
* @link http://nelson6e65.github.io/php_nml/
* @since v0.5.0
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
* */

if (!extension_loaded('gettext')) {
return;
}

// Set the path of translations
bindtextdomain(NML_GETTEXT_DOMAIN, __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Locale');
16 changes: 10 additions & 6 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* Content:
* - Global functions definition for NML.
*
* Copyright © 2016-2021 Nelson Martell (http://nelson6e65.github.io)
* Copyright © 2016-2024 Nelson Martell (http://nelson6e65.github.io)
*
* Licensed under The MIT License (MIT)
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright 2016-2021 Nelson Martell
* @copyright 2016-2024 Nelson Martell
* @link http://nelson6e65.github.io/php_nml/
* @since 0.6.0
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
Expand All @@ -38,7 +38,7 @@
* */
function msg(string $message, ...$args): string
{
$translated = \dgettext(NML_GETTEXT_DOMAIN, $message);
$translated = extension_loaded('gettext') ? \dgettext(NML_GETTEXT_DOMAIN, $message) : $message;

$data = $args;

Expand All @@ -49,7 +49,6 @@ function msg(string $message, ...$args): string
return Text::format($translated, $data);
}


/**
* Busca un mensaje único, en singular y plural, traducido en el dominio 'nml'.
* El mensaje puede contener cadenas de formato.
Expand All @@ -64,10 +63,16 @@ function msg(string $message, ...$args): string
* @return string
* @since 0.6.0
* @see \dngettext()
* @internal
* */
function nmsg(string $singular, string $plural, int $n, ...$args): string
{
$translated = \dngettext(NML_GETTEXT_DOMAIN, $singular, $plural, $n);
if (extension_loaded('gettext')) {
$translated = \dngettext(NML_GETTEXT_DOMAIN, $singular, $plural, $n);
} else {
// Simple implementation without Gettext
$translated = $n === 1 ? $singular : $plural;
}

$data = $args;

Expand All @@ -78,7 +83,6 @@ function nmsg(string $singular, string $plural, int $n, ...$args): string
return Text::format($translated, $data);
}


/**
* Obtiene el tipo del objeto especificado.
* Es un alias para el constructor de la clase Type.
Expand Down

0 comments on commit 8fb2ec5

Please sign in to comment.