def _HardcodeInTable(table, is_bundle_module, shared_resources_allowlist): translations_package = None if is_bundle_module: # A separate top level package will be added to the resources, which # contains only locale specific resources. The package ID of the locale # resources is hardcoded to SHARED_LIBRARY_HARDCODED_ID. This causes # resources in locale splits to all get assigned # SHARED_LIBRARY_HARDCODED_ID as their package ID, which prevents a bug # in shared library bundles where each split APK gets a separate dynamic # ID, and cannot be accessed by the main APK. translations_package = Resources_pb2.Package() translations_package.package_id.id = SHARED_LIBRARY_HARDCODED_ID translations_package.package_name = (table.package[0].package_name + '_translations') # These resources are allowed in the base resources, since they are needed # by WebView. allowed_resource_names = set() if shared_resources_allowlist: allowed_resource_names = set( resource_utils.GetRTxtStringResourceNames(shared_resources_allowlist)) for package in table.package: for _type in package.type: for entry in _type.entry: for config_value in entry.config_value: _ProcessProtoValue(config_value.value) if translations_package is not None: locale_type = _SplitLocaleResourceType(_type, allowed_resource_names) if locale_type: translations_package.type.add().CopyFrom(locale_type) if translations_package is not None: table.package.add().CopyFrom(translations_package)
def _RemoveUnwantedLocalizedStrings(dep_subdirs, options): """Remove localized strings that should not go into the final output. Args: dep_subdirs: List of resource dependency directories. options: Command-line options namespace. """ # Collect locale and file paths from the existing subdirs. # The following variable maps Android locale names to # sets of corresponding xml file paths. locale_to_files_map = collections.defaultdict(set) for directory in dep_subdirs: for f in _IterFiles(directory): locale = resource_utils.FindLocaleInStringResourceFilePath(f) if locale: locale_to_files_map[locale].add(f) all_locales = set(locale_to_files_map) # Set A: wanted locales, either all of them or the # list provided by --locale-allowlist. wanted_locales = all_locales if options.locale_allowlist: wanted_locales = _ToAndroidLocales(options.locale_allowlist, options.support_zh_hk) # Set B: shared resources locales, which is either set A # or the list provided by --shared-resources-allowlist-locales shared_resources_locales = wanted_locales shared_names_allowlist = set() if options.shared_resources_allowlist_locales: shared_names_allowlist = set( resource_utils.GetRTxtStringResourceNames( options.shared_resources_allowlist)) shared_resources_locales = _ToAndroidLocales( options.shared_resources_allowlist_locales, options.support_zh_hk) # Remove any file that belongs to a locale not covered by # either A or B. removable_locales = (all_locales - wanted_locales - shared_resources_locales) for locale in removable_locales: for path in locale_to_files_map[locale]: os.remove(path) # For any locale in B but not in A, only keep the shared # resource strings in each file. for locale in shared_resources_locales - wanted_locales: for path in locale_to_files_map[locale]: resource_utils.FilterAndroidResourceStringsXml( path, lambda x: x in shared_names_allowlist) # For any locale in A but not in B, only keep the strings # that are _not_ from shared resources in the file. for locale in wanted_locales - shared_resources_locales: for path in locale_to_files_map[locale]: resource_utils.FilterAndroidResourceStringsXml( path, lambda x: x not in shared_names_allowlist)