xref: /aosp_15_r20/external/cronet/base/win/embedded_i18n/generate_embedded_i18n.gni (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2018 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/config/chrome_build.gni")
6import("//build/config/locales.gni")
7
8# This template defines a rule to generate localized string .rc and .h files
9# that can be embedded directly into a library or executable if desired
10#
11# Parameters
12#
13#   grd_files_info
14#       List of tuples that contains information about the all the .grd files
15#       from which to extract the strings.
16#       Each tuple contains 4 elements:
17#         1. The folder containing the grd file.
18#         2. The name of the grd file without an extension.
19#         3. The path relative to the grd file where the xtb files are stored.
20#         4. The expected locales of the xtb files available in the xtb
21#            relative path (can use default_embedded_i18_locales defined in
22#            this file). This is just the locale portion of the filename so
23#            if a file like google_strings_en-US.xtb is expected then this
24#            list should contain "en-US".
25#
26#   output_file_name_base
27#       The base name of the generated header / rc file that will contain the
28#       extracted string information. The files will be output to:
29#       "$target_gen_dir/$output_file_name_base.h" and
30#       "$target_gen_dir/$output_file_name_base.rc".
31#
32#   first_resource_id (optional)
33#       The starting resource ID for the generated string resources.
34#       Defaults to 1600.
35#
36#   extractor_datafile (optional)
37#       The python file to execute that contains definition of the specific strings
38#       to extract from the source .grd. This file can contain an array STRING_IDS
39#       that lists all the IDs that will be extracted for all brand targets.
40#       It can optionally also contain a dictionary MODE_SPECIFIC_STRINGS that
41#       contains mode specific strings for certain brand targets.
42#       MODE_SPECIFIC_STRINGS is only valid if STRING_IDS is specified.
43#       Will not be passed to the generation script if it is undefined.
44#
45#   branding (optional)
46#       The branding used to determine specific string extractions.
47#       Will not be passed to the generation script if it is undefined.
48#
49#   deps (optional)
50#   visibility (optional)
51
52# Locales in |all_chrome_locales| are for pak file format.  We need to convert them
53# to the xtb version.
54default_embedded_i18_locales = all_chrome_locales - [
55                                 "en-US",
56                                 "he",
57                                 "nb",
58                               ] +
59                               [
60                                 "iw",
61                                 "no",
62                               ] - pseudolocales
63
64template("generate_embedded_i18n") {
65  assert(defined(invoker.grd_files_info),
66         "Grd file information must be defined for $target_name")
67  assert(defined(invoker.output_file_name_base),
68         "Output file name base must be defined for $target_name")
69
70  if (defined(invoker.extractor_datafile)) {
71    extractor_datafile = invoker.extractor_datafile
72  }
73
74  grd_files_info = invoker.grd_files_info
75  output_file_name_base = invoker.output_file_name_base
76
77  if (defined(invoker.branding)) {
78    branding = invoker.branding
79  }
80
81  first_resource_id = "1600"
82  if (defined(invoker.first_resource_id)) {
83    first_resource_id = invoker.first_resource_id
84  }
85
86  action(target_name) {
87    script = "//base/win/embedded_i18n/create_string_rc.py"
88    inputs = []
89
90    output_dir = rebase_path(target_gen_dir, root_build_dir)
91
92    output_header = "$target_gen_dir/$output_file_name_base.h"
93    output_rc = "$target_gen_dir/$output_file_name_base.rc"
94
95    outputs = [
96      output_header,
97      output_rc,
98    ]
99
100    args = [
101      "--header-file",
102      "$output_dir/$output_file_name_base.h",
103      "--rc-file",
104      "$output_dir/$output_file_name_base.rc",
105      "--first-resource-id",
106      first_resource_id,
107    ]
108
109    foreach(grd_file_info, grd_files_info) {
110      grdfile_folder = grd_file_info[0]
111      grdfile_name = grd_file_info[1]
112      xtb_relative_path = grd_file_info[2]
113      grd_file = "$grdfile_folder/$grdfile_name.grd"
114      resource_path = "$grdfile_folder/$xtb_relative_path"
115      inputs += [ grd_file ]
116
117      args += [
118        "-i",
119        rebase_path(grd_file, root_build_dir),
120        "-r",
121        xtb_relative_path,
122      ]
123
124      # Lists must be reset to empty before being assigned a new list
125      xtb_locales = []
126      xtb_locales = grd_file_info[3]
127
128      foreach(locale, xtb_locales) {
129        expected_xtb_input = "${resource_path}/${grdfile_name}_${locale}.xtb"
130        args += [
131          "-x",
132          rebase_path(expected_xtb_input, root_build_dir),
133        ]
134        inputs += [ expected_xtb_input ]
135      }
136    }
137
138    if (defined(extractor_datafile)) {
139      inputs += [ extractor_datafile ]
140
141      args += [
142        "--extract-datafile",
143        rebase_path(extractor_datafile, root_build_dir),
144      ]
145    }
146
147    if (defined(branding)) {
148      args += [
149        "-b",
150        branding,
151      ]
152    }
153
154    forward_variables_from(invoker, [ "deps" ])
155    forward_variables_from(invoker, [ "visibility" ])
156  }
157}
158