-
-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Support for localized strings #120
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hello @dimtrovich thank you. it definitely is a big and significant work so kudos to that.
however i feel like we shouldn't have it in the library as it is quite niche and a bit away from focus of cli package.
instead can we make it as a plugin (which you can put under your git repo and we can link it up in readme here with instructions for i18n)
cc @kodie what's your thoughts on it? for me it is big feature &/or change surface to be in the core |
Hello @adhocore. Sorry for the delay. The other problem I have with this is that it will be quite difficult to capture your hard-coded text and then translate it... There's a bit of text in all the classes and also between the special formatting tags (for example, in the error stack display method, like With this, I think it will be impossible to externalize the translation system. On the other hand, you could remove the translation files and leave just English, and the developer who wants a different language could extend it as I mentioned in the Readme. |
right now there is no way to externalize translation because thats not the focus/usecase of this pkg. as for this pr, the surface of code change is too big for not necessarily must have feature. i would like to have it bare minimum like so: namespace Ahc\Cli;
class Application
{
public static $locale = 'en';
public static $locales = [];
public static function addLocale(string $loc, array $texts, bool $default)
{
if ($default) Application::$locale = $loc;
Application::$locales[$loc] = $texts;
}
}
function t(string $text, array $args): string
{
$loc = Application::$locale;
// EN is already there as original text (no keys needed)
$txt = Application::$locales[$loc][$txt] ?? $txt;
return sprintf($txt, ...$args);
}
// internally cli pkg uses t().
// eg: in https://github.com/adhocore/php-cli/blob/main/src/Input/Command.php#L199
// throw new InvalidParameterException('Only last argument can be variadic');
// will be
// throw new InvalidParameterException(t('Only last argument can be variadic')); externally end users would use it like so: Application::addLocale('fr', [
'Only last argument can be variadic' => 'Seul le dernier argument peut être variadique',
], true); now since there is no translation key in code, we just list them in the readme from: |
also the sprintf positional arguments needs to be handled as explained here: https://www.php.net/manual/en/function.sprintf.php#refsect1-function.sprintf-examples example text: 'The current (%d) is greater than the total (%d).' |
I do like the compromise of allowing string translation, but not having the cli package need to maintain the different translations. What @adhocore suggests makes translations work similar to how the styles system works now which is nice. There are built in defaults, but everything can be customized. My one slight update to what @adhocore suggested would be to make it possible to customize the built-in local as well. For example if I would like the "Show help" description on the |
It's true that I'm not a big fan of having free-to-air channels as table keys. However, I like it because at least it means that all the text is directly readable internally. |
@adhocore which means that we'll have to remove the embedded translations from the package and leave it up to the developer. |
yes we have to remove and thats what makes this change less disruptive and all-compatible and no, it's not the only translation system that uses real en texts as keys (https://www.hiddentechies.com/blog/magento-2/magento2-generate-language-translation-csv/) |
WordPress also works that way: https://developer.wordpress.org/reference/functions/__/ |
Just an idea too, once it's all set up, translations could be stored in gists much like I did with colors: https://gist.github.com/kodie/b63881ccf4368e9e00c0c3277779524a that way the tool can still be translated without having to do it manually every time. |
done |
done. https://gist.github.com/dimtrovich/1597c16d5c74334e68eef15a4e7ba3fd however, it looks like you didn't put it in the doc. it might as well be in the doc so we don't have to look too hard. |
src/Helper/InflectsString.php
Outdated
/** | ||
* Translates a message using the translator. | ||
*/ | ||
public static function translate(string $text, array $args = []): string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it could be a simple function t()
or __()
under namespace Ahc\cli
(not method) :)
then can be imported as use function Ahc\Cli\t
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh ok but i had striked this one and left another note 😀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand. Can you please explain?
the function t()
simple or a method in the InflectsString trait. because now I don't understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, now I understand better. I hadn't paid attention to the link you provided. I'm going to manage the argument positioning
So, in the case of the t()
function, should we leave it or go back to the previous translate method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its ok as it is, we just need to consider the sprintf for wrapping up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, shouldn't positioning only be managed for texts with more than one argument?
For example, this is relevant for The current (%d) is greater than the total (%d).
but much less so for Command %s not found
.
I would like to leave Command %s not found
unchanged but change The current (%d) is greater than the total (%d).
to The current (%1$d) is greater than the total (%2$d).
.
or we'll make everything the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya thats fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
gist sample was also updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for now no need to have t()
but the positional sprintf must be handled :)
how? I think this has already been taken into account. 🤔🤔 |
Putting it in the readme is a good idea! I came up with it after the PR was merged in |
Introduction of translation support.
Until now, all the texts generated by the CLI have been in English. The problem is that some people run commands in other languages (French in my case) and so you get a mixture of English and French, which isn't very cool to see.
This subject has already been discussed here #64
With this PR, the texts are translated and the developer can therefore use the language best suited to his project
en (default)
fr - French
es - Spanish
de - German
ru - Russian