def test_token_without_content(self): template = FileMock(""" {{ }} """) result = extract_vue(template, DEFAULT_KEYWORDS.keys(), [], TEST_OPTIONS) self.assertEqual(list(result), [])
def test_colon_directives(self): template = FileMock(""" <div :html="gettext('Sometext')"></div> """) result = extract_vue(template, DEFAULT_KEYWORDS.keys(), [], TEST_OPTIONS) self.assertListEqual(list(result), [(2, u'gettext', u'Sometext', [])])
def test_underscore(self): template = FileMock(""" <div> {{ _("Bar") }} </div> """) result = extract_vue(template, DEFAULT_KEYWORDS.keys(), [], TEST_OPTIONS) self.assertEqual(list(result), [(3, '_', u'Bar', [])])
def test_gettext(self): template = FileMock(""" <div> {{ gettext('Foo') }} </div> """) result = extract_vue(template, DEFAULT_KEYWORDS.keys(), [], TEST_OPTIONS) self.assertEqual(list(result), [(3, u'gettext', u"Foo", [])])
def test_no_messages(self): template = FileMock(b""" <div> <h1>Foo</h1> <p>Lore ipsum delore gettext() ...</p> </div> """) result = extract_vue(template, DEFAULT_KEYWORDS.keys(), [], TEST_OPTIONS) self.assertEqual(list(), list(result))
def test_directives_with_inner_colon_tag(self): template = FileMock(""" <div :text="gettext('Sometext')"> {{ gettext('Hello') }} </div> """) result = extract_vue(template, DEFAULT_KEYWORDS.keys(), [], TEST_OPTIONS) self.assertListEqual(list(result), [(2, u'gettext', u'Sometext', []), (3, u'gettext', u'Hello', [])])
def test_ngettext(self): template = FileMock(b""" <div> {{ ngettext('Foo', 'Foos', 1) }} </div> """) result = extract_vue(template, DEFAULT_KEYWORDS.keys(), [], TEST_OPTIONS) self.assertEqual(list(result), [ (3, u'ngettext', (u"Foo", u"Foos"), []), ])
def test_commas(self): template = FileMock(b""" {{ gettext('Hello, User') }} {{ gettext("You're") }} {{ gettext("You\\"re") }} """) result = extract_vue(template, DEFAULT_KEYWORDS.keys(), [], TEST_OPTIONS) self.assertEqual(list(result), [ (2, u'gettext', u'Hello, User', []), (3, u'gettext', u"You're", []), (4, u'gettext', u"You\"re", []), ])
def test_gettext_with_parameter(self): template = FileMock(b""" <li> {{ gettext('{number} season').replace("{number}", season) }} {{ gettext('Processed by filter')|somefilter }} {{ somefunc(gettext('Processed by function')) }} {{ gettext('Foo') + gettext('bar') }} </li> """) result = extract_vue(template, DEFAULT_KEYWORDS.keys(), [], TEST_OPTIONS) self.assertEqual(list(result), [ (3, u'gettext', u'{number} season', []), (4, u'gettext', u'Processed by filter', []), (5, u'gettext', u'Processed by function', []), (6, u'gettext', u'Foo', []), (6, u'gettext', u'bar', []), ])
def extract_extrajs(fileobj, keywords, comment_tags, options): """Extract template literal placeholders and filters from Javascript files. :param fileobj: the file-like the messages should be extracted from :param keywords: a list of keywords (i.e. function names) that should be recognize as translation functions :param comment_tags: a list of translator tags to search for and include in the results :param options: a dictionary of additional options (optional) :return: an iterator over ``(lineno, funcname, message, comments)`` :rtype: ``iterator`` """ encoding = options.get('encoding', 'utf-8') c = fileobj.read().decode(encoding=encoding) filtpat = re.compile('t\._f\("gettext"\)', re.UNICODE) c = filtpat.sub('gettext', c) comppat = re.compile(r'(\$\{gettext\((.*?)\)\})', re.UNICODE) c = comppat.sub(r'`+gettext(\g<2>)+`', c, re.UNICODE) for i in extract_javascript(BytesIO(c.encode(encoding=encoding)), DEFAULT_KEYWORDS.keys(), comment_tags, options): if i: yield (i[0], i[1], i[2], i[3])
def setup(self): self.keywords = list(DEFAULT_KEYWORDS.keys())
def generate_keywords(): """Generates gettext keywords list""" keywords = set(BABEL_KEYWORDS.keys()) keywords.add('_lazy') # FIXME: Add other keywords here return list(keywords)
def test_empty_template(self): template = FileMock("") result = extract_vue(template, DEFAULT_KEYWORDS.keys(), [], TEST_OPTIONS) self.assertEqual(list(), list(result))
def setup(self): self.keywords = DEFAULT_KEYWORDS.keys()