Skip to content

Commit

Permalink
fix: speed up Lang.pm initialization, save 2 seconds x 100 tests (#9197)
Browse files Browse the repository at this point in the history
fix: speed up Lang.pm init
  • Loading branch information
stephanegigandet committed Oct 27, 2023
1 parent 4424e55 commit 230b1d6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
55 changes: 42 additions & 13 deletions lib/ProductOpener/Lang.pm
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ sub lang_in_other_lc ($target_lc, $stringid) {

$log->info("initialize", {data_root => $data_root}) if $log->is_info();

# Load stored %Lang from Lang.sto
# Load stored %Lang from Lang.sto and Lang_tags.sto

my $path = "$data_root/data/Lang.${server_domain}.sto";
if (-e $path) {
Expand All @@ -275,27 +275,49 @@ if (-e $path) {
}

$log->info("Loaded languages", {langs => (scalar @Langs)}) if $log->is_info();
sleep(1) if $log->is_info();
}
else {
$log->warn("Language translation file does not exist, \%Lang will be empty. Run scripts/build_lang.pm to fix this.",
{path => $path})
if $log->is_warn();
}

# Tags types to path components in URLS: in ascii, lowercase, unaccented,
# transliterated (in Roman characters)
#
# Note: a lot of plurals are currently missing below, commented-out are
# the singulars that need to be changed to plurals
my ($tag_type_singular_ref, $tag_type_plural_ref)
= ProductOpener::I18N::split_tags(ProductOpener::I18N::read_po_files("$data_root/po/tags/"));
%tag_type_singular = %{$tag_type_singular_ref};
%tag_type_plural = %{$tag_type_plural_ref};
$path = "$data_root/data/Lang_tags.${server_domain}.sto";
if (-e $path) {

$log->info("Loading tag types <=> singular and plural translated paths", {path => $path}) if $log->is_info();
my $tag_type_data_ref = retrieve($path);
$log->info("Loaded tag types <=> singular and plural translated paths", {path => $path}) if $log->is_info();

%tag_type_singular = %{$tag_type_data_ref->{tag_type_singular}};
%tag_type_plural = %{$tag_type_data_ref->{tag_type_plural}};
%tag_type_from_singular = %{$tag_type_data_ref->{tag_type_from_singular}};
%tag_type_from_plural = %{$tag_type_data_ref->{tag_type_from_plural}};
}
else {
$log->warn("Language translation file for tags does not exist. Run scripts/build_lang.pm to fix this.",
{path => $path})
if $log->is_warn();
}

# Taxonomies that can have debug, prev, and next versions
# (older feature to generate tags using multiple versions of a taxonomy, currently not used)
my @debug_taxonomies = ("categories", "labels", "additives");

{
# Build hashes to map a translated tag type (e.g. "cat茅gorie") in singular or plural to the tag type (e.g. "categories")

sub build_lang_tags() {

# Tags types to path components in URLS: in ascii, lowercase, unaccented,
# transliterated (in Roman characters)
#
# Note: a lot of plurals are currently missing below, commented-out are
# the singulars that need to be changed to plurals
my ($tag_type_singular_ref, $tag_type_plural_ref)
= ProductOpener::I18N::split_tags(ProductOpener::I18N::read_po_files("$data_root/po/tags/"));
%tag_type_singular = %{$tag_type_singular_ref};
%tag_type_plural = %{$tag_type_plural_ref};

foreach my $l (@Langs) {

foreach my $taxonomy (@debug_taxonomies) {
Expand Down Expand Up @@ -355,7 +377,14 @@ my @debug_taxonomies = ("categories", "labels", "additives");
}

}

return (
{
tag_type_singular => \%tag_type_singular,
tag_type_plural => \%tag_type_plural,
tag_type_from_singular => \%tag_type_from_singular,
tag_type_from_plural => \%tag_type_from_plural
}
);
}

# initialize languages values:
Expand Down
2 changes: 2 additions & 0 deletions scripts/build_lang.pl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
# Tags.pm builds the %Languages hash of languages from the languages taxonomy

ProductOpener::Lang::build_lang(\%Languages);
my $tags_ref = ProductOpener::Lang::build_lang_tags();

# use $server_domain in part of the name so that we have different files
# when 2 instances of Product Opener share the same $data_root
Expand All @@ -48,6 +49,7 @@
mkdir("$data_root/data", 0755) or die("Could not create target directory $data_root/data : $!\n");
}
store("$data_root/data/Lang.${server_domain}.sto", \%Lang);
store("$data_root/data/Lang_tags.${server_domain}.sto", $tags_ref);

# Generate JSON files for JavaScript I18N
ProductOpener::Lang::build_json();
Expand Down

0 comments on commit 230b1d6

Please sign in to comment.