smhg / gettext-swig Goto Github PK
View Code? Open in Web Editor NEWExtract translatable strings from Swig template strings.
License: MIT License
Extract translatable strings from Swig template strings.
License: MIT License
[edit] I updated this comment soon after posting it.
I'm working on gettext-volt to integrate into xgettext-template
and I was very confused because it appeared as though it was detecting strings but then I couldn't figure out how that could because it isn't wrapped in {{}}
.
This will detect 1 string, because of the ,
after ('Features')
:
{%- set menus = [
'features': this.translate._('Features'),
'admin': this.translate._('Admin')
] -%}
This will detect 0 strings:
{%- set menus = [
'features': this.translate._('Features')
] -%}
I'm not quite sure why that comma is necessary but it seems to me that to be 100% that gettext-swig
would need to to have more than one regex pattern. Swig shouldn't be detecting any of these.
swig-i18n-abide supports a rather nice %s, %d format with {% trans "String %(foobar)s with string replacements" %}
.
I think that '%(var)s' format might be idea for variable strings. So then the Swig parser would just need to collapse '%(var)(s|d)' to '%(s|d)' for the extracted message.
First of all, thanks for the job you've done so far.
It would be awesome if parser could extract comments as well, as in:
{# TRANSLATORS Here is some info for translators #}
and then just pass -c=TRANSLATORS
to the command.
I'd contribute a PR myself instead of writing NFR, but I know jack shit about it.
P.S. I actually use Volt as my template engine, but this extractor beats the hell out of xgettext-volt
as it supports contexts unlike latter.
{{t('You\'ve successfully created a Webird account. To activate it, please click below to verify your email address.')}}
comes over as You\\
.
I think that there needs to be two different regexs. One would ignore \'
inside of single quotes and the other would ignore \"
within double quotes.
Its valid to have no spaces after the {{
. I set it at up to a maximum of 3 spaces just because it could help if someone quickly pasted it in while testing. I was having issues with getting it to run until I browsed at the source.
this.expressionPattern = new RegExp([
// '{{ +',
'{{ {0,3}',
'(' + Object.keys(keywordSpec).map(escapeRegExp).join('|') + ')',
'\\(',
'([\\s\\S]*?)',
'\\)',
' {0,3}}}'
// ' +}}'
].join(''), 'g');
}
Sorry, I'm too lazy for a pull request plus I figure that you need to reversion npm anyways if you like it. Personally, I need this.
The parser appears to fail on all of these:
Single quote escapes:
<p>{{ ngettext('\'quote', 'quotes', amount) }}</p>
<p>{{ ngettext('\'quote\'', 'quotes', amount) }}</p>
<p>{{ ngettext('quote', '\'quotes', amount) }}</p>
<p>{{ ngettext('quote', '\'quotes\'', amount) }}</p>
Double quote escapes:
<p>{{ ngettext("\"quote", "quotes", amount) }}</p>
<p>{{ ngettext("\"quote\"", "quotes", amount) }}</p>
<p>{{ ngettext("quote", "\"quotes", amount) }}</p>
<p>{{ ngettext("quote", "\"quotes\"", amount) }}</p>
It appears that {{ }} always uses paranthesis and that {% %} doesn't. They are intended for different uses but that so far appears to be the only constant as different engines use them for different functionality. By supporting both of these formats the Swig parser can be used across the entire line of Django family templates (and there are several varieties).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.