xgettext 不提取 HTML 属性中的字符串 [英] xgettext does not extract string in HTML attribute

查看:12
本文介绍了xgettext 不提取 HTML 属性中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm working on a Wordpress site using Timber as templating engine (twig for Wordpress). I want to translate the interface using the Wordpress gettext mechanism via PoEdit (uses xgettext).

Template snippet:

<form action="/{{ current_language }}/api/search/{{ viewModel.currentSuperCategory.key }}"
  method="post"
  class="search-results__search"
  id="search-form">
<input placeholder="{{ __('City, region, ZIP', text_domain) }}&hellip;"
       class="input search-results__search__input"
       name="search-query"
       type="text"
       data-search-autocomplete />

I'm using PoEdit to extract the strings from the *.twig file.

It is working if I place {{ __('City, region, ZIP', text_domain) }} on a separate line in the template, but it is not working when placed in the input placeholder as shown in the snippet above.

I configured PoEdit as explained here: https://github.com/jarednova/timber/wiki/Text-Cookbook#generating-po-files-using-poedit

Does anybody have an idea why that is the case? Is xgettext ignoring strings inside of HTML attributes?

Another example:

In the following case the strings get extracted for translation:

<i class="fa fa-envelope"></i>
{{ __('Search subscription', text_domain) }}
{{ __('Save free search subscription', text_domain) }}

But not in the following case:

<i class="fa fa-envelope"></i>
<span data-rt-xs="{{ __('Search subscription', text_domain) }}" data-rt-lg="{{ __('Save free search subscription', text_domain) }}"></span>

解决方案

That’s because you are not actually extracting from "HTML atttributes", but from a template language that xgettext has no support for. To work around that you are, I’m guessing, using the gross Python hack from that page. In effect, you are lying to xgettext/Poedit about what the file contains, with results that are, frankly, to be expected. Is the file a valid Python file? No, it isn’t, it’s Twig. You can’t really expect it to be parsed correctly when you mislead the tool about the language.

Instead, I recommend doing what that page also recommends, and as a better way:

A nicer solution is to use Twig-Gettext-Extractor, a special Twig parser to Poedit. The linked page contains instructions on how to set it up.

这篇关于xgettext 不提取 HTML 属性中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆