Homec4science

Add an optional pht() callback for collecting string frequencies

Authored by epriestley <git@epriestley.com> on Nov 8 2016, 15:46.

Description

Add an optional pht() callback for collecting string frequencies

Summary:
Ref T5267. For translations, it's useful to have frequency data so the most frequently used strings can be prioritized for translation -- common UI strings are much more important to translate than big blocks of setup/configuration text or obscure error messages.

Add an optional callback so we can collect which strings are actually translated at runtime and build a dataset for prioritizing strings for translation.

Test Plan:
Applied this simple hack to phabricator/:

diff --git a/src/infrastructure/env/PhabricatorEnv.php b/src/infrastructure/env/PhabricatorEnv.php
index 31fd116..9e08a21 100644
--- a/src/infrastructure/env/PhabricatorEnv.php
+++ b/src/infrastructure/env/PhabricatorEnv.php
@@ -59,6 +59,8 @@ final class PhabricatorEnv extends Phobject {
   private static $readOnly;
   private static $readOnlyReason;

+  public static $frequency = array();
+
   const READONLY_CONFIG = 'config';
   const READONLY_UNREACHABLE = 'unreachable';
   const READONLY_SEVERED = 'severed';
@@ -166,7 +168,8 @@ final class PhabricatorEnv extends Phobject {

       PhutilTranslator::getInstance()
         ->setLocale($locale)
-        ->setTranslations($override + $translations);
+        ->setTranslations($override + $translations)
+        ->setWillTranslateCallback('PhabricatorEnv::willTranslate');

       self::$localeCode = $locale_code;
     } catch (Exception $ex) {
@@ -174,6 +177,13 @@ final class PhabricatorEnv extends Phobject {
     }
   }

+  public static function willTranslate($text) {
+    if (empty(self::$frequency[$text])) {
+      self::$frequency[$text] = 0;
+    }
+    self::$frequency[$text]++;
+  }
+
   private static function buildConfigurationSourceStack($config_optional) {
     self::dropConfigCache();

diff --git a/webroot/index.php b/webroot/index.php
index 59e5b71..d57dd35 100644
--- a/webroot/index.php
+++ b/webroot/index.php
@@ -15,6 +15,14 @@ try {
   try {
     PhabricatorStartup::beginStartupPhase('run');
     AphrontApplicationConfiguration::runHTTPRequest($sink);
+
+    $freq = PhabricatorEnv::$frequency;
+    asort($freq);
+    Filesystem::writeFile(
+      '/tmp/pht_frequency.json',
+      id(new PhutilJSON())
+        ->encodeFormatted($freq));
+
   } catch (Exception $ex) {
     try {
       $response = new AphrontUnhandledExceptionResponse();

Got a datafile like this:

{
  "Piece of Eight": 1,
  "Haypence": 1,
  "Disable DarkConsole": 1,
  "Yellow Medal": 1,
  "Doubloon": 1,
  "Mountain of Wealth": 1,
  "Baby Tequila": 1,
  "Evil Spooky Haunted Tree": 1,
  "Pterodactyl": 1,
  ...

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T5267

Differential Revision: https://secure.phabricator.com/D16821

Details

Committed
epriestley <git@epriestley.com>Nov 8 2016, 16:22
Pushed
aubortMar 17 2017, 12:03
Parents
rPHU5d8e090fe1e2: Slightly improve some prose diffs
Branches
Unknown
Tags
Unknown

Event Timeline

epriestley <git@epriestley.com> committed rPHU5dbc2a8e01f6: Add an optional pht() callback for collecting string frequencies (authored by epriestley <git@epriestley.com>).Nov 8 2016, 16:22