Exemple #1
0
/**
 * gtr_po_save_file:
 * @po: a #GtrPo
 * @error: a GError to manage the exceptions
 *
 * It saves the po file and if there are any problem it stores the error
 * in @error.
 **/
void
gtr_po_save_file (GtrPo * po, GError ** error)
{
  struct po_xerror_handler handler;
  gchar *filename;
  gchar *msg_error;
  GtrHeader *header;
  GtrPoPrivate *priv = gtr_po_get_instance_private (po);

  /*
   * Initialice the handler error.
   */
  handler.xerror = &on_gettext_po_xerror;
  handler.xerror2 = &on_gettext_po_xerror2;

  filename = g_file_get_path (priv->location);

  if (g_str_has_suffix (filename, ".pot"))
    {
      // Remove suffix
      filename[strlen (filename) - 4] = '\0';
      g_set_error (error,
                   GTR_PO_ERROR,
                   GTR_PO_ERROR_FILENAME,
                   _("You are saving a file with a .pot extension.\n"
                     "Pot files are generated by the compilation process.\n"
                     "Your file should likely be named “%s.po”."), filename);
      g_free (filename);
      return;
    }


  if (is_read_only (filename))
    {
      g_set_error (error,
                   GTR_PO_ERROR,
                   GTR_PO_ERROR_READONLY,
                   _("The file %s is read-only, and can not be overwritten"),
                   filename);
      g_free (filename);
      return;
    }

  /* Save header fields into msg */
  header = gtr_po_get_header (po);
  gtr_header_update_header (header);

  /*
   * Check if the file is right
   */
  msg_error = gtr_po_check_po_file (po);
  if (msg_error != NULL)
    {
      g_set_error (error,
                   GTR_PO_ERROR,
                   GTR_PO_ERROR_GETTEXT,
                   _("There is an error in the PO file: %s"),
                   msg_error);
      g_free (msg_error);
      g_free (filename);
      return;
    }

  if (!po_file_write (gtr_po_get_po_file (po), filename, &handler))
    {
      g_set_error (error,
                   GTR_PO_ERROR,
                   GTR_PO_ERROR_FILENAME,
                   _("There was an error writing the PO file: %s"),
                   message_error);
      g_free (message_error);
      g_free (filename);
      return;
    }
  g_free (filename);

  /* If we are here everything is ok and we can set the state as saved */
  gtr_po_set_state (po, GTR_PO_STATE_SAVED);

  /*
   * If the warn if fuzzy option is enabled we have to show an error
   */
  /*if (gtr_prefs_manager_get_warn_if_fuzzy () && priv->fuzzy)
     {
     g_set_error (error,
     GTR_PO_ERROR,
     GTR_PO_ERROR_OTHER,
     ngettext ("File %s\ncontains %d fuzzy message",
     "File %s\ncontains %d fuzzy messages",
     priv->fuzzy),
     priv->fuzzy);
     } */
}
int main(int argc, char **argv)
{
	po_file_t po;
	po_message_iterator_t it;
	const char * const * domains;

	if(!argv[1])
	{
		fprintf(stderr,"usage: %s <pofile> [ask|fix]\n",argv[0]);
		return 0;
	}

	if(argv[2])
	{
		if(!strncmp(argv[2], "ask", 3))
		{
			solution_mode=1;
		} else if(!strncmp(argv[2], "fix", 3)) {
			solution_mode=2;
		} else {
			fprintf(stderr,"usage: %s <pofile> [ask|fix]\n",argv[0]);
			return 0;
		}
	}

	po = po_file_read(argv[1], &po_xerror_handler);

	if(!po)
	{
		fprintf(stderr,"Couldn't read the input po file\n");
		return 0;
	}

	domains = po_file_domains(po);

	if(!domains)
	{
		fprintf(stderr,"Couldn't find the message domains in the po file\n");
		return 0;
	}

	while(*domains)
	{
		it = po_message_iterator(po,*domains);
		process_messages(it);
		po_message_iterator_free(it);
		domains++;
	}

	if(total_errors == 0 && total_warnings == 0)
	{
		fprintf(stderr,"No errors found\n");
	} else if(solution_mode==0) {
		fprintf(stderr,"%d warnings, %d errors\n",total_warnings,total_errors);
	} else {
		fprintf(stderr,"%d warnings, %d errors, %d fixes, %d removed, %d ignored\n",total_warnings,total_errors, total_fix, total_remove, total_ignore);
		if(total_fix || total_remove)
		{
			int s = 0;
			printf("Commit changes to file? (y)es, (n)o:");
			while(s != 'y' && s != 'n') s = getchar();
			if(s == 'y')
			{
				if(po_file_write(po, argv[1], &po_xerror_handler)) printf("Changes committed.");
				else printf("Error writing file.");
			} else {
				printf("Changes discarded.");
			}
		}
	}

	po_file_free(po);

	return 0;
}