6 <& /elements/tr-input-locale-text.html,
7 cgi => $cgi, # needed to preserve values in error redirect
13 And in edit/process/foo.html:
14 <& elements/process.html,
16 process_locale => 'myfield',
19 'object' needs to be an FS::Record subclass instance for a table that has
20 a '_msgcat' localization table. For a table "foo" where "foo.myfield"
21 contains some customer-visible label (in the default locale),
22 "foo_msgcat.myfield" contains the translation of that label for a customer
23 locale. The foreign key in foo_msgcat must have the same name as the primary
26 Currently only a single field can be localized this way; including this
27 element more than once in the form will lead to conflicts. This is how
28 it should work; if at some point we need to localize several fields of the
29 same record, we should modify this element to show multiple inputs for each
36 my $object = delete $opt{object};
37 my $field = delete $opt{field};
39 # identify our locales
40 my $conf = FS::Conf->new;
41 my $default_locale = $conf->config('locale') || 'en_';
42 my @locales = grep { ! /^$default_locale/ } $conf->config('available-locales');
44 my $label = delete $opt{label};
45 my %labels = map { $_ => "$label—".FS::Locales->description($_) }
47 @locales = sort { $labels{$a} cmp $labels{$b} } @locales;
50 # where are the msgcat records?
51 my $msgcat_table = $object->table . '_msgcat';
52 my $msgcat_pkey = dbdef->table($msgcat_table)->primary_key;
55 # find existing msgcat records, if any, and record their message values
57 my $pkey = $object->primary_key;
58 my $pkeyval = $object->get($pkey);
59 if ($pkeyval) { # of course if this is a new record there won't be any
60 my @linked = qsearch($msgcat_table, { $pkey => $pkeyval });
62 $curr_values{ $_->locale } = $_->get( $field );
63 $msgcat_pkeyvals{ $_->locale } = $_->get( $msgcat_pkey );
67 # sticky-on-error the locale inputs
68 if( my $cgi = $opt{cgi} ) {
70 # they're named 'foomsgnum0_locale' and 'foomsgnum0_myfield'
71 while ( my $locale = $cgi->param($msgcat_pkey . $i . '_locale') ) {
72 my $value = $cgi->param($msgcat_pkey . $i . '_' . $field);
73 $curr_values{ $locale } = $value;
78 # compat with tr-input-text for styling
79 my $cell_style = $opt{'cell_style'} ? 'STYLE="'. $opt{'cell_style'}. '"' : '';
81 my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
85 % # pass through %opt on all of these to retain formatting
86 % # one tr, td, and input for the default locale
87 <& tr-input-text.html,
92 % # and one for each of the others
94 % foreach my $locale (@locales) {
95 % my $basename = $msgcat_pkey . $i;
96 % my $lfield = $basename . '_' . $field;
99 'id' => $lfield, # uniqueness
100 'label' => $labels{$locale}
102 <TD <% $colspan %><% $cell_style %> ID="<% $lfield %>_input0">
104 'field' => $basename,
105 'curr_value' => $msgcat_pkeyvals{$locale},
106 # will be empty if this is a new record and/or new locale, that's fine
109 'field' => $basename . '_locale',
110 'curr_value' => $locale,
115 'curr_value' => $curr_values{$locale},
120 % } # foreach $locale