Skip to content

Commit 8fb2ec5

Browse files
committed
feat: make gettext features optional
1 parent 2fc2d6b commit 8fb2ec5

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

autoload.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
* - File to perform manual autoload. For non composer instalation, must be
88
* required at app initialization.
99
*
10-
* Copyright © 2015-2021 Nelson Martell (http://nelson6e65.github.io)
10+
* Copyright © 2015-2024 Nelson Martell (http://nelson6e65.github.io)
1111
*
1212
* Licensed under The MIT License (MIT)
1313
* For full copyright and license information, please see the LICENSE
1414
* Redistributions of files must retain the above copyright notice.
1515
*
16-
* @copyright 2015-2021 Nelson Martell
16+
* @copyright 2015-2024 Nelson Martell
1717
* @link http://nelson6e65.github.io/php_nml/
1818
* @since 0.3.0
1919
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
@@ -67,17 +67,18 @@ function autoloadNelsonMartellLibrary($class)
6767
} elseif (is_file($path . '.inc')) {
6868
$path .= '.inc';
6969
} else {
70-
$msg = 'Unable to auto-load "%s" class in Nelson Martell Library (NML): "%s" file was not found.' .
71-
' You can see the API documentation (http://nelson6e65.github.io/php_nml/api) in order to check ' .
72-
' availability of all classes/namespaces in NML. Note: If you are using "NelsonMartell" as main namespace' .
73-
' in a file that not belongs to NML, you should include it before to load "NML/autoload.php" or,' .
74-
' using SPL autoload features, register autoload function for that class(es) using "prepend" argument for' .
75-
' spl_autoload_register function set to TRUE.';
70+
$msg =
71+
'Unable to auto-load "%s" class in Nelson Martell Library (NML): "%s" file was not found.' .
72+
' You can see the API documentation (http://nelson6e65.github.io/php_nml/api) in order to check ' .
73+
' availability of all classes/namespaces in NML. Note: If you are using "NelsonMartell" as main namespace' .
74+
' in a file that not belongs to NML, you should include it before to load "NML/autoload.php" or,' .
75+
' using SPL autoload features, register autoload function for that class(es) using "prepend" argument for' .
76+
' spl_autoload_register function set to TRUE.';
7677

77-
throw new Exception(sprintf(dgettext('nml', $msg), $class, $path));
78+
throw new Exception(sprintf(NelsonMartell\msg($msg), $class, $path));
7879
}
7980

80-
require_once($path);
81+
require_once $path;
8182
}
8283

8384
spl_autoload_register('autoloadNelsonMartellLibrary');

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
"docs": "http://nelson6e65.github.io/php_nml/api"
2525
},
2626
"require": {
27-
"php": ">=7.2"
27+
"php": ">=7.2",
28+
"ext-mbstring": ">=7.2",
29+
"ext-json": "*"
30+
},
31+
"suggest": {
32+
"ext-gettext": "Required to enable translations and some functions"
2833
},
2934
"require-dev": {
3035
"squizlabs/php_codesniffer": "^3.0",

config/bootstrap.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
* For full copyright and license information, please see the LICENSE
1313
* Redistributions of files must retain the above copyright notice.
1414
*
15-
* @copyright 2015-2021 Nelson Martell
15+
* @copyright 2015-2024 Nelson Martell
1616
* @link http://nelson6e65.github.io/php_nml/
1717
* @since v0.5.0
1818
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
1919
* */
2020

21+
if (!extension_loaded('gettext')) {
22+
return;
23+
}
24+
2125
// Set the path of translations
2226
bindtextdomain(NML_GETTEXT_DOMAIN, __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Locale');

src/functions.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* Content:
77
* - Global functions definition for NML.
88
*
9-
* Copyright © 2016-2021 Nelson Martell (http://nelson6e65.github.io)
9+
* Copyright © 2016-2024 Nelson Martell (http://nelson6e65.github.io)
1010
*
1111
* Licensed under The MIT License (MIT)
1212
* For full copyright and license information, please see the LICENSE
1313
* Redistributions of files must retain the above copyright notice.
1414
*
15-
* @copyright 2016-2021 Nelson Martell
15+
* @copyright 2016-2024 Nelson Martell
1616
* @link http://nelson6e65.github.io/php_nml/
1717
* @since 0.6.0
1818
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
@@ -38,7 +38,7 @@
3838
* */
3939
function msg(string $message, ...$args): string
4040
{
41-
$translated = \dgettext(NML_GETTEXT_DOMAIN, $message);
41+
$translated = extension_loaded('gettext') ? \dgettext(NML_GETTEXT_DOMAIN, $message) : $message;
4242

4343
$data = $args;
4444

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

52-
5352
/**
5453
* Busca un mensaje único, en singular y plural, traducido en el dominio 'nml'.
5554
* El mensaje puede contener cadenas de formato.
@@ -64,10 +63,16 @@ function msg(string $message, ...$args): string
6463
* @return string
6564
* @since 0.6.0
6665
* @see \dngettext()
66+
* @internal
6767
* */
6868
function nmsg(string $singular, string $plural, int $n, ...$args): string
6969
{
70-
$translated = \dngettext(NML_GETTEXT_DOMAIN, $singular, $plural, $n);
70+
if (extension_loaded('gettext')) {
71+
$translated = \dngettext(NML_GETTEXT_DOMAIN, $singular, $plural, $n);
72+
} else {
73+
// Simple implementation without Gettext
74+
$translated = $n === 1 ? $singular : $plural;
75+
}
7176

7277
$data = $args;
7378

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

81-
8286
/**
8387
* Obtiene el tipo del objeto especificado.
8488
* Es un alias para el constructor de la clase Type.

0 commit comments

Comments
 (0)