ruby-gettext / gettext

Gettext gem is a pure Ruby Localization(L10n) library and tool which is modeled after the GNU gettext package.
https://ruby-gettext.github.io/
69 stars 28 forks source link

gettext does not work with glade #63

Closed ellcs closed 4 years ago

ellcs commented 5 years ago

Hello there! Before i start complaining, i would like to thank you for your work! :) I do use the glade version 3.22.1 and the most recent version of the gettext ruby gem. Within the project, i do use GetText::Tools::Task to define a rake task. In order to fetch various translation changes within the glade file.

# Somewhere within the rake file
require 'gettext/tools/task'
GetText::Tools::Task.define do |task|
  task.po_base_directory = 'po'
  task.mo_base_directory = 'locale'
  task.files = FileList['widgets/**/*.glade']
  task.domain = "test_domain"
end

All widgets, locales and po files are valid. When i run rake gettext:po:update to fetch changes within the glade file, i receive an error: widget is not glade-2.0 format..

The problem is located in file lib/gettext/tools/parser/glade.rb. gettext validates the gladefile, with two checks. The second check causes trouble. It assumes that the first two lines look like this (I copied it from gettext/samples/hello_glade2.glade):

<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">

I tried to open the sample file, with glade. But glade threw an error. My glade file started with those lines:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->

Workaround

I simply added a second Regex to check if the second line starts with <!-- Generated with glade. And guess what, my po files are correctly updated and i do not receive errors anymore. I will attach the patch.

ellcs commented 5 years ago
diff --git a/lib/gettext/tools/parser/glade.rb b/lib/gettext/tools/parser/glade.rb
index 31fce5a..daf4f04 100644
--- a/lib/gettext/tools/parser/glade.rb
+++ b/lib/gettext/tools/parser/glade.rb
@@ -21,20 +21,29 @@ module GetText

     class << self
       XML_RE = /<\?xml/
-      GLADE_RE = /glade-2.0.dtd/
+      GLADE_RELEASE_OLD = /glade-2.0.dtd/
+      GLADE_RELEASE = /<!-- Generated with glade/

       def target?(file) # :nodoc:
         data = IO.readlines(file)
-        if XML_RE =~ data[0] and GLADE_RE =~ data[1]
+        if starts_with_xml_tag?(data) && includes_glade_tag?(data)
           true
+        elsif File.extname(file) == '.glade'
+          raise _("`%{file}' is not glade-2.0 format.") % {:file => file}
         else
-          if File.extname(file) == '.glade'
-            raise _("`%{file}' is not glade-2.0 format.") % {:file => file}
-          end
           false
         end
       end

+      def starts_with_xml_tag?(data)
+        data[0] =~ XML_RE
+      end
+
+      def includes_glade_tag?(data)
+        second_line = data[1]
+        GLADE_RELEASE_OLD =~ second_line || GLADE_RELEASE =~ second_line
+      end
+
       def parse(path, options={})
         parser = new(path, options)
         parser.parse
kou commented 5 years ago

Recent Glade uses GtkBuilder's UI definitions format that uses .ui as extension.

We need to add support for .ui.

ellcs commented 5 years ago

@kou i'd like to resolve this issue. Do you think those two steps are enough for a PR?

However, i wonder how to provide useful tests or test files. The old glade test files seem quite out of date.

kou commented 5 years ago

We should not remove the current Glade parser. We should add a new parser for the GtkBuilder: lib/gettext/tools/parser/gtk_builder.rb

kou commented 5 years ago

We need to add a new .ui files for test. We can create those files by recent Glade.

kou commented 4 years ago

I've implemented.

ellcs commented 4 years ago

Thank you! I will have a look. :+1: