tree-sitter-htmldjango icon indicating copy to clipboard operation
tree-sitter-htmldjango copied to clipboard

As a person who wants to translate strings, I would like support for "string | filter"

Open jhthorsen opened this issue 1 year ago • 3 comments

Thank you for adding django support to tree-sitter!

I was wondering if it could be possible to add support for the syntax below:

<html>
  {{ "foo" }}
  {{ 'foo'|filter }}
  {{ "foo"|filter:argument }}
</html>

My usecase is to use it for translations:

{{ 'Hello %1!' | i18n }}

jhthorsen avatar May 24 '24 06:05 jhthorsen

Thanks for the issue! The Django docs are very specific regarding variable names:

Variable names consist of any combination of alphanumeric characters and the underscore ("_") but may not start with an underscore, and may not be a number. The dot (".") also appears in variable sections, although that has a special meaning, as indicated below. Importantly, you cannot have spaces or punctuation characters in variable names.

It never occurred to me that any other characters (i.e. quotes) should be checked for, since I only considered the case of variables names (and optional filters) contained within {{ }} and not string literals like your example.

Can you find any documentation that demonstrates this syntax?

interdependence avatar May 27 '24 21:05 interdependence

Happened to be reading the Django docs right after seeing this post and came across the example of {{ my_date|date:"Y-m-d" }} in Template Filters,

Espencer123 avatar Sep 12 '24 22:09 Espencer123

That case should work fine as is, since the string is used as an argument to the date filter.

interdependence avatar Sep 14 '24 17:09 interdependence

Sorry for not following up 😄 In my case, it's not a variable on the left side of the pipe. It's simple a string literal. Like "Hello " in the example below:

{{ "Hello " ~ name ~ "!" }}

I couldn't find the specification, but this sure works:

 {{ "string literatl" | filter:argument }}

jhthorsen avatar Mar 07 '25 12:03 jhthorsen

Since your use case is translation, should you not be using the translate template tag?

interdependence avatar Apr 14 '25 18:04 interdependence

The point is not if I’m using it for translation or not. The idea is to have valid syntax highlighting for strings.

jhthorsen avatar Apr 15 '25 12:04 jhthorsen

Apologies for being particular about this but sometimes when issues are opened it is because a user is using incorrect syntax, not a fault of the grammar. In this case the syntax is undocumented (at least I can't find any official reference), but does work based on my own testing as well, so it should be supported. I will work on implementing it.

interdependence avatar Apr 15 '25 14:04 interdependence

Thank you šŸ™

jhthorsen avatar Apr 15 '25 20:04 jhthorsen

I have implemented support for string literals inside expressions (3a64316).

interdependence avatar Apr 16 '25 18:04 interdependence