-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Transliterate Twig filter for Grav #3891
base: develop
Are you sure you want to change the base?
Transliterate Twig filter for Grav #3891
Conversation
this seems to perform a similar task to the existing |
Hi @rhukster , Do you refer to this code in Utils.php?:
I'm not sure where this method is used in the rest of the project (I can't find it in the code of the other files in the system folder), nor how I should create the alias you propose. Any suggestions? |
Ok. It seems I found a solution you might like. In the GravExtension.php file we add this:
And in Utils.php the following:
Ok. It seems I found a solution you might like. In the GravExtension.php file we add this: new TwigFilter('to_ascii', [Utils::class, 'toAscii']), And in Utils.php the following:
Is it okay if I make these changes to this PR? |
If you accept these suggestions, all that remains is to add the necessary help to the Grav documentation, the Twig Filters section. |
sounds good! |
What if |
I think this maybe why i ended up going with the basic version i have already in Utils. Best solution in this scenarios is to check if function exists, if so, use best version, else fallback to basic (existing string replace version). |
Well, I'm not an expert programmer, I don't even know the whole internal structure of Grav in depth, but I'm trying to advance little by little in my knowledge of it. https://www.dopethemes.com/utf8_decode-replacement-modern-php-encoding-alternatives/ What do you suggest @dimayakovlev , to add a filter that can have the purpose proposed here (like other CMS like Drupal have, with transliterate filter) |
I've been testing on my local Grav and so far it's worked without any issues. There's nothing else to add to Grav's code to make use of this filter, so I understand that the iconv() function shouldn't be an impediment. What's more, it includes error handling, adding //IGNORE in case of using non-allowed characters. In the PHP manual, it clearly states that iconv() is included in the C standard library (libc-bin) |
If preferred, you could use the mbstring library, which appears as requirements of php for Grav, or add the iconv() library to these requirements. |
@pmoreno-rodriguez, for transliteration it is better to use class |
The problem of transliteration is much more complicated than it seems. There is no single standard for transliteration, and different rules can be used in the same language. If the Twig transliteration filter is included in Grav CMS, it should be possible to define your own transliteration table to get an acceptable result. |
Ok. At this point, I only dare to make a basic proposal, made with the Transliterator class, which I have tested with different Russian, Bulgarian, etc. phrases and apparently they work. If it is feasible to introduce this filter in Grav, I will be happy to look for more information and improve it, but if not, I think I will spend my time on other projects that I still have pending for Grav. Here is the code based on the php transliterator class:
The function could be called Transliterate instead of toAscii.
where title is: Как дела в Мадриде?, with transliterate filter is converted into Kak dela v Madrid |
In my opinion this is a good idea for a plugin. Use And if we're talking about simple transliteration, we don't need to remove characters, we just need to replace letters of one alphabet with letters of another, in our case, letters of the English alphabet. So you can create two filters:
|
Good. I'll see what I can do with your last suggestion. I will try to create a plugin with basic options and see how it works |
Hello @rhukster and @dimayakovlev. I already have a base for a future plugin to include the filter to transliterate text in Grav. This is the GitHub repository so you can contribute your suggestions. Thanks in advance. |
Hi again. Today I've uploaded a new version of Transliterate plugin, with full README, some improvements for transliterate and ASCII filters. Please, @rhukster and @dimayakovlev, take a look to see if it is a useful plugin to include in the Grav plugin repository |
Transliterate is a Grav Twig filter that converts accented characters to their ASCII equivalents. For example, 'Ä' will become 'A'.