[yast-devel] Converting YCP data files into YAML, continue removing YCP
Hi all, The ruby conversion did not touch the YCP data files (the files which are installed into /usr/share/YaST2/data and are read using SCR::Read(.target.yast2/.ycp, "foo.ycp") call.) These data files usually contain additional data like hardware databases, additional data mapping... There are several reasons why we did not convert them: - "foo.ycp" in the SCR call would not exist after the conversion, there would be "foo.rb" - .target.yast2 agent parses YCP files, it would fail with Ruby content - Ruby format is not the best format for storing data But to get rid of the YCP interpreter completely (our long term goal) we need to convert these YCP data files anyway. For this reason I have created a simple "ycp2yml" client which reads a specified YCP file and writes it's value into a YAML file. It is included in the latest devtools. It's very easy, it just evaluates the YCP file via SCR.Read, coverts to YAML by calling to_yaml method and saves the result to a file. That means it's not a 1:1 copy, e.g. comments are not transferred, translation marks are lost,... But in many cases it is enough. Of course, you can patch the YAML file manually after the conversion. Example usage: /sbin/yast2 ycp2yml consolefonts.ycp consolefonts.yml Then replace the original YCP file in Git (don't forget to fix Makefile.am and *.spec.in) and use it this way: require 'yaml' # add this if not already there Yast.import "Directory" ... # original code: data = ... SCR.Read(path(".target.yast2"), "foo.ycp") data = YAML.load_file(Directory.datadir + "/foo.yml") rescue nil # nil is the default value if an exception is raised (missing file, # broken/inconsistent, read error...) and is backward compatible. # You can use different default if needed. Note: if you use translations in the original YCP file then you need to use different solution, e.g. convert it into a Ruby file and make it Yast include. You can look at these pull requests: https://github.com/yast/yast-sound/pull/2 https://github.com/yast/yast-ntp-client/pull/10 Enjoy! -- Best Regards Ladislav Slezák Yast Developer ------------------------------------------------------------------------ SUSE LINUX, s.r.o. e-mail: lslezak@suse.cz Lihovarská 1060/12 tel: +420 284 028 960 190 00 Prague 9 fax: +420 284 028 951 Czech Republic http://www.suse.cz/ -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org To contact the owner, e-mail: yast-devel+owner@opensuse.org
Dne 8.8.2013 08:18, Ladislav Slezak napsal(a):
Then replace the original YCP file in Git (don't forget to fix Makefile.am and *.spec.in) and use it this way:
require 'yaml'
Forgot to mention that the YAML module is part of core Ruby [1], this does not add any new external gem/package dependency... [1] http://ruby-doc.org/stdlib-2.0/libdoc/yaml/rdoc/YAML.html -- Ladislav Slezák Appliance department / YaST Developer Lihovarská 1060/12 190 00 Prague 9 / Czech Republic tel: +420 284 028 960 lslezak@suse.com SUSE -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org To contact the owner, e-mail: yast-devel+owner@opensuse.org
V Thu, 08 Aug 2013 08:18:01 +0200 Ladislav Slezak <lslezak@suse.cz> napsáno:
Note: if you use translations in the original YCP file then you need to use different solution, e.g. convert it into a Ruby file and make it Yast include.
Still not usable for data files of yast2-country: currently there are explicitly loaded via target.ycp each time when the language is changed so the new translation is available during run-time. I guess we cannot force load the include file when we need to re-translate those files. Is there any ruby solution for that? Jiri -- Jiri Suchomel SUSE LINUX, s.r.o. Lihovarská 1060/12 tel: +420 284 028 960 190 00 Praha 9, Czech Republic http://www.suse.cz -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org To contact the owner, e-mail: yast-devel+owner@opensuse.org
Dne 8.8.2013 11:59, Jiří Suchomel napsal(a):
V Thu, 08 Aug 2013 08:18:01 +0200 Ladislav Slezak <lslezak@suse.cz> napsáno: [...] Still not usable for data files of yast2-country: currently there are explicitly loaded via target.ycp each time when the language is changed so the new translation is available during run-time.
I guess we cannot force load the include file when we need to re-translate those files. Is there any ruby solution for that?
A possible solution could be to wrap the YAML in an ERB template, in ERB you can put any Ruby code: foo.yml.erb: --- foo: <%= _("bar") %> Then it could be read using YAML.load(ERB.new(File.read("foo.yml.erb")).result) (See e.g. http://urgetopunt.com/rails/2009/09/12/yaml-config-with-erb.html) Ruby Gettext even supports ERB, so it should be easy to extract translatable strings from ERB files. But the example is not complete, we would need to "escape" the translated string to make sure not to break the YAML format... Another possibility is to use a user data type feature in YAML, you can actually convert any object to YAML: Object.new.to_yaml => "--- !ruby/object {}\n" We could create e.g. TranslatableString class which would handle translations. But in this case extracting the translatable texts from YAML would be complicated, we would likely need a new specific parser for that. (BTW in both cases the YAML files became unportable, you cannot read them using e.g. perl-YAML library or something else. But that should not be a problem, YCP files were unreadable outside Yast anyway...) I already discussed that in the past with Josef, he told me that it should be somehow possible to use translations in YAML files. So let's see when he's back from vacation... -- Ladislav Slezák Appliance department / YaST Developer Lihovarská 1060/12 190 00 Prague 9 / Czech Republic tel: +420 284 028 960 lslezak@suse.com SUSE -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org To contact the owner, e-mail: yast-devel+owner@opensuse.org
On Thu, 08 Aug 2013 13:27:25 +0200 Ladislav Slezak <lslezak@suse.cz> wrote:
Dne 8.8.2013 11:59, Jiří Suchomel napsal(a):
V Thu, 08 Aug 2013 08:18:01 +0200 Ladislav Slezak <lslezak@suse.cz> napsáno: [...] Still not usable for data files of yast2-country: currently there are explicitly loaded via target.ycp each time when the language is changed so the new translation is available during run-time.
I guess we cannot force load the include file when we need to re-translate those files. Is there any ruby solution for that?
A possible solution could be to wrap the YAML in an ERB template, in ERB you can put any Ruby code:
foo.yml.erb: --- foo: <%= _("bar") %>
Then it could be read using
YAML.load(ERB.new(File.read("foo.yml.erb")).result)
(See e.g. http://urgetopunt.com/rails/2009/09/12/yaml-config-with-erb.html)
Ruby Gettext even supports ERB, so it should be easy to extract translatable strings from ERB files.
But the example is not complete, we would need to "escape" the translated string to make sure not to break the YAML format...
This looks like reasonable solution,because you need to re-evaluate file everytime again as it is not trivial to change language set during runtime.
Another possibility is to use a user data type feature in YAML, you can actually convert any object to YAML:
Object.new.to_yaml => "--- !ruby/object {}\n"
We could create e.g. TranslatableString class which would handle translations.
But in this case extracting the translatable texts from YAML would be complicated, we would likely need a new specific parser for that.
(BTW in both cases the YAML files became unportable, you cannot read them using e.g. perl-YAML library or something else. But that should not be a problem, YCP files were unreadable outside Yast anyway...)
the first solution looks better for me.
I already discussed that in the past with Josef, he told me that it should be somehow possible to use translations in YAML files. So let's see when he's back from vacation...
Well, RoR use different solution, that they have file.en.yaml with en string and file.cs.yaml with different language set and use their own I18n, so question is if we want to stay with gettext, but probably yes, to make live easier for translators. Josef -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org To contact the owner, e-mail: yast-devel+owner@opensuse.org
participants (3)
-
Jiří Suchomel
-
Josef Reidinger
-
Ladislav Slezak