HomePhabricator

Add an optional pht() callback for collecting string frequencies

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

Provenance
epriestleyAuthored on Nov 8 2016, 2:46 PM
epriestleyPushed on Nov 8 2016, 3:22 PM
Reviewer
chad
Differential Revision
D16821: Add an optional pht() callback for collecting string frequencies
Parents
rPHU5d8e090fe1e2: Slightly improve some prose diffs
Branches
Unknown
Tags
Unknown
Tasks
T5267: Localize Phabricator
Build Status
Buildable 14398
Build 18753: Run Core Tests