Permalink
Browse files

feature(developers): Always show human-readable translations

Even when the `show_strings` setting is turned on, it's nice to
be able to navigate the app. This makes it so that the translation
keys are just appended to the translated string, rather than replacing
them entirely.

Before: `developers🏷show_strings`

After: `Show raw translation strings (developers🏷show_strings)`

Fixes #8834
  • Loading branch information...
ewinslow committed Aug 11, 2015
1 parent c44c5c4 commit 43c19644aa7a30525990c2b24770056273e6c7d0
Showing with 32 additions and 3 deletions.
  1. +32 −3 mod/developers/start.php
View
@@ -39,9 +39,9 @@ function developers_process_settings() {
}
if (!empty($settings['show_strings'])) {
- // first and last in case a plugin registers a translation in an init method
- elgg_register_event_handler('init', 'system', 'developers_clear_strings', 1000);
- elgg_register_event_handler('init', 'system', 'developers_clear_strings', 1);
+ // Beginning and end to make sure both early-rendered and late-loaded translations get included
+ elgg_register_event_handler('init', 'system', 'developers_decorate_all_translations', 1);
+ elgg_register_event_handler('init', 'system', 'developers_decorate_all_translations', 1000);
}
if (!empty($settings['show_modules'])) {
@@ -102,8 +102,37 @@ function developers_setup_menu() {
}
}
+/**
+ * Adds debug info to all translatable strings.
+ */
+function developers_decorate_all_translations() {
+ $language = get_language();
+ _developers_decorate_translations($language);
+ _developers_decorate_translations('en');
+}
+
+/**
+ * Appends " ($key)" to all strings for the given language.
+ *
+ * This function checks if the suffix has already been added so it is idempotent
+ *
+ * @param string $language Language code like "en"
+ */
+function _developers_decorate_translations($language) {
+ foreach ($GLOBALS['_ELGG']->translations[$language] as $key => &$value) {
+ $needle = " ($key)";
+
+ // if $value doesn't already end with " ($key)", append it
+ if (substr($value, -strlen($needle)) !== $needle) {
+ $value .= $needle;
+ }
+ }
+}
+
/**
* Clear all the strings so the raw descriptor strings are displayed
+ *
+ * @deprecated Superceded by developers_decorate_all_translations
*/
function developers_clear_strings() {
$language = get_language();

0 comments on commit 43c1964

Please sign in to comment.