$entries Translation_Entry objects (`singular`, `context`, `translations`). WordPress 6.5 and later returns a plain list; older releases key them by "context\4singular", which is the fallback. * @param string $pluralForms Gettext Plural-Forms header, e.g. "nplurals=2; plural=n != 1;". * @param list|null $only Keys to keep (see JsStringCatalog::keys()); null keeps every translated entry. * @return array|null Null when there is nothing to translate. */ public static function build(iterable $entries, string $locale, string $pluralForms, ?array $only = null): ?array { $wanted = $only === null ? null : array_flip($only); $data = []; foreach ($entries as $key => $entry) { $translations = array_values(array_map('strval', $entry->translations ?? [])); if ($translations === [] || implode('', $translations) === '') { continue; } $singular = $entry->singular ?? null; if (is_string($singular) && $singular !== '') { $context = $entry->context ?? null; $key = is_string($context) && $context !== '' ? $context . "\4" . $singular : $singular; } if ($wanted !== null && !isset($wanted[(string) $key])) { continue; } $data[(string) $key] = $translations; } if ($data === []) { return null; } return ['' => ['domain' => self::DOMAIN, 'lang' => $locale, 'plural_forms' => $pluralForms]] + $data; } /** * The inline script that registers the data before the app runs, or null when there is none. * * @param array|null $data */ public static function script(?array $data): ?string { if ($data === null) { return null; } return 'wp.i18n.setLocaleData(' . wp_json_encode($data, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP) . ', ' . wp_json_encode(self::DOMAIN) . ');'; } }