我是YAML的新手,我正在尝试编写一些语言翻译测试.我的想法是我有一个YAML文件,其中包含相同文本的英语和威尔士语版本.我想知道如何在YAML中的字符串中使用正则表达式?
例:
我有这个基本测试:
#Gherkin And I expect to see payment confirmation for "Dave" #Step Def And /^I expect to see payment confirmation for "(.*?)"$/ do |user| expect(page).to have_text "Payment successful for #{user} - Reference: 000000" end
YAML参赛作品:
"Payment successful for Dave reference: 000000" - welsh: "Taliad llwyddiannus ar gyfer Dave Cyfeirnod: 000000"
我可以在该YAML条目中参数化Dave,因为测试套件涵盖了数百个用户.
谢谢
如果要将文本插入到YAML中,则需要替换要插入的单词,%{keyword}
其中关键字是一个键,用于指示要在其中插入的数据.
如果您使用Rails,您将使用I18n指南的本节中t
概述的帮助方法.
# The YAML file en: payment_succeeded: "Payment successful for %{first_name} reference: %{reference_number}" wh: payment_succeeded: "Taliad llwyddiannus ar gyfer %{first_name} Cyfeirnod: %{reference_number}" # Rails Controller / View / Model t(:payment_succeeded, first_name: 'Dave', reference_number: '000000')
如果您想要纯ruby实现,可以使用该Kernel::format
方法.
# The YAML file # NOTE: This can be in any format, as long as you use %{keyword} in places where you want the data to be interpolated translations: english: "Payment successful for %{first_name} reference: %{reference_number}" welsh: "Taliad llwyddiannus ar gyfer %{first_name} Cyfeirnod: %{reference_number}" # Ruby translations = YAML.load(File.read('translations.yaml')) string = format(translations[:translations][:welsh], first_name: 'Dave', reference_number: '000000')
希望这可以帮助.