#!/usr/bin/env perl
# PODNAME: po2json
# ABSTRACT: Convert a PO file to the JSON data for Locale::Simple in the browser
our $VERSION = '0.200';

use strict;
use warnings;

use File::Basename ();
use Locale::Simple::PO2JSON qw( po2data data2json );

my $usage = "Usage: po2json [-p] FILE.po > FILE.json\n"
  . "  -p  indent the output and wrap it in the file name as domain\n";

my $pretty = 0;
if ( @ARGV and $ARGV[0] eq '-p' ) {
    shift @ARGV;
    $pretty = 1;
}
# Deprecated: one line on STDERR, STDOUT stays the clean JSON. Only -p has an
# exact replacement; the plain form has none, the notice says what differs.
my $instead = $pretty
  ? 'locale-simple po2json --pretty FILE.po'
  : 'locale-simple po2json FILE.po (no exact equivalent: it wraps the data as { "FILE": ... })';
print STDERR 'po2json is deprecated, use: ' . $instead . "\n";
if ( !@ARGV or $ARGV[0] =~ /^(?:-h|--help)$/ or @ARGV > 1 ) {
    print STDERR $usage;
    exit 1;
}

my ($file) = @ARGV;
my $json = eval {
    $pretty
      ? data2json( po2data( $file, domain => File::Basename::basename( $file, '.po', '.pot' ) ), pretty => 1 )
      : data2json( po2data($file) );
};
if ( !defined $json ) {
    print STDERR $@;
    exit 1;
}
print $json, "\n";

__END__

=pod

=encoding UTF-8

=head1 NAME

po2json - Convert a PO file to the JSON data for Locale::Simple in the browser

=head1 VERSION

version 0.200

=head1 SYNOPSIS

B<Deprecated:> use L<locale-simple> instead. C<locale-simple po2json --pretty
FILE.po> writes exactly what C<po2json -p FILE.po> writes. C<po2json FILE.po>
has no exact equivalent: C<locale-simple po2json FILE.po> writes the same data
wrapped as C<{ "FILE": ... }> (C<--domain NAME> sets the key), the shape the
JavaScript C<loadLocaleData> takes. Every call prints a deprecation notice on
STDERR; the output and the exit codes are unchanged.

  po2json messages.po > messages.json
  po2json -p messages.po > messages.json

=head1 DESCRIPTION

Reads a PO file and writes the JSON data for the JavaScript side of
L<Locale::Simple> to STDOUT. Entries without a translation, fuzzy entries and
obsolete entries are left out, as msgfmt leaves them out of a C<.mo> file.

With C<-p> the output is indented and wrapped in the file name (without
C<.po> or C<.pot>) as domain. C<-h> prints the usage and exits with C<1>, as
does an unreadable or malformed file, with the reason on STDERR.

See L<locale-simple> for the same conversion as a subcommand.

=head1 SUPPORT

=head2 Issues

Please report bugs and feature requests at
L<https://github.com/Getty/locale-simple/issues>.

=head1 CONTRIBUTING

Contributions are welcome! Please fork the repository and submit a pull request.

=head1 AUTHOR

Torsten Raudssus <getty@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.

This is free software, licensed under:

  The MIT (X11) License

=cut
