[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bug#53981: language-chooser



Hi. :)

> > I think that loading both files may not work in the current .trm structure.
> > We have to change the procedure to generate .trm files to include English 
> > messages if the translated messages are not available.
> Exactly.

Here is the patch for trim-mo.c in pointerize-0.3, to let trim-mo
accept the optional second argument as a reference .mo file:

diff -ruN ../build/pointerize-0.3/src/trim-mo.c ./pointerize-0.3/src/trim-mo.c
--- ../build/pointerize-0.3/src/trim-mo.c	Mon Mar  8 05:18:53 1999
+++ ./pointerize-0.3/src/trim-mo.c	Sat Jan 15 14:08:26 2000
@@ -26,14 +26,16 @@
 extern struct loaded_domain *load_domain (char *domain_file);
 
 static void write_table (struct loaded_domain *domain, int outputOrig);
+static int write_table_2 (struct loaded_domain *domain, 
+                         struct loaded_domain *ref_domain);
 
 int main(int argc, char **argv) {
-	struct loaded_domain *domain;
+	struct loaded_domain *domain, *ref_domain;
 	int outputOrig;
 
 	outputOrig = 0;
-	if (argc != 2) {
-		fprintf(stderr,"Usage: %s MO-file\n",argv[0]);
+	if (argc != 2 && argc != 3) {
+		fprintf(stderr,"Usage: %s MO-file (Reference.mo)\n",argv[0]);
 		exit(-1);
 	}
 	domain=load_domain(argv[1]);
@@ -41,8 +43,17 @@
 		fprintf(stderr,"Error loading file: %s\n",argv[1]);
 		exit(-1);
 	}
-	write_table(domain, outputOrig);
-	exit(0);
+	if (argc == 2) {
+	  write_table(domain, outputOrig);
+	  exit(0);
+	} else { /* (argc == 3) */
+	  ref_domain=load_domain(argv[2]);
+	  if (ref_domain == NULL) {
+		fprintf(stderr,"Error loading file: %s\n",argv[2]);
+		exit(-1);
+	  }
+	  exit(write_table_2(domain, ref_domain)); 
+	}
 }
 
 static void
@@ -93,3 +104,97 @@
       fwrite (ptr, len + 1, 1, stdout);
     }
 }
+
+
+static int
+write_table_2 (struct loaded_domain *domain, 
+		struct loaded_domain *ref_domain)
+{
+  size_t cnt;
+  nls_uint32 nstrings_ref, nstrings, offset;
+  char **messages;
+
+  nstrings_ref = ref_domain->nstrings;
+  nstrings = domain->nstrings;
+
+  /* Write the header out.  */
+  fwrite (&nstrings_ref, sizeof (nstrings), 1, stdout);
+
+  /* allocate the space for messages */
+  if((messages = (char **)calloc (nstrings_ref, sizeof (char *))) == NULL) {
+    return (-1); /* can not allocate memory */
+  }
+
+  /* Now Get the Reference strings.  */
+  for (cnt = 0; cnt < nstrings_ref; ++cnt)
+    {
+      messages[cnt] = (char *) (ref_domain->data +
+                      ref_domain->orig_tab[cnt].offset);
+    }
+
+  /* Now, array messages[] has all the Reference strings,
+   * and array len_msgs[] has all the length of strings in
+   * messages[]. We use these to find the translated
+   * messages using descOrig and descTrans and binary search
+   * in the sorted array of messages.
+   */
+
+  /* These codes are based on dcgettext.c in GNU gettext */
+  for (cnt = 0; cnt < nstrings_ref; ++cnt)
+    {
+      size_t bottom, top, act;
+      const char *ref_ptr;
+
+      ref_ptr = messages[cnt];
+      bottom = 0;
+      top = nstrings;
+
+      while (bottom < top)
+        {
+          int cmp_val;
+
+          act = (bottom + top) / 2;
+          cmp_val = strcmp (ref_ptr, (char *)(domain->data+
+                                     domain->orig_tab[act].offset));
+          if (cmp_val < 0)
+            top = act;
+          else if (cmp_val > 0)
+            bottom = act + 1;
+          else
+            break;
+        }
+
+      /* If an translation is found, replace messages[cnt] using it */
+      if (bottom < top) {
+         messages[cnt] = (char *) domain->data + 
+                                  domain->trans_tab[act].offset;
+      }
+    }
+
+  /* Set offset to first byte after all the tables.  */
+  offset = sizeof (nstrings) + nstrings_ref * sizeof (offset);
+
+  /* Write out starting offset for all strings.  */
+  for (cnt = 0; cnt < nstrings_ref; ++cnt)
+    {
+      size_t len;
+
+      len = strlen (messages[cnt]);
+
+      fwrite (&offset, sizeof (offset), 1, stdout);
+      offset += len + 1;
+    }
+
+  /* Now write all the strings.  */
+  for (cnt = 0; cnt < nstrings_ref; ++cnt)
+    {
+      size_t len;
+
+      len = strlen (messages[cnt]);
+
+      fwrite (messages[cnt], len + 1, 1, stdout);
+    }
+
+  return (0); /* Success! */
+}
+

Using this modified trim-mo, and do "trim-mo LANG.mo C.mo >LANG.trm".
Then we can get .trm file of mixed messages translated and not-translated.

Hope this will help our boot-floppies.

-- 
  Taketoshi Sano: <sano@debian.org>,<sano@debian.or.jp>,<kgh12351@nifty.ne.jp>


Reply to: