Commit 6bd1e2b5 authored by Gabor Hojtsy's avatar Gabor Hojtsy
Browse files

Issue #2322839 by herom: Unify YAML translation extraction, and allow extension by contrib.

parent 220a61e0
Loading
Loading
Loading
Loading
+53 −3
Changes for potx.inc: 53 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -1855,10 +1855,51 @@ function _potx_find_translation_annotations($file, $save_callback) {
  }
}

function _potx_init_translation_patterns() {
/**
 * Initialize the list of translation patterns for YAML files.
 *
 * The patterns are loaded from a file named "yaml_translation_patterns.yml" in
 * potx's root. Contrib projects can also include their own
 * "yaml_translation_patterns.yml" file in their root folder to specify
 * translatable strings in their own custom yaml files.
 *
 * @param string $path
 *   The path to look for a custom "yaml_translation_patterns.yml" file.
 */
function _potx_init_yaml_translation_patterns($path = '') {
  global $yaml_translation_patterns;
  $yaml_translation_patterns = array();

  $content = Yaml::parse(file_get_contents(drupal_get_path('module', 'potx') . '/yaml_translation_patterns.yml'));
  $default_patterns = drupal_get_path('module', 'potx') . '/yaml_translation_patterns.yml';
  _potx_load_yaml_translation_patterns($default_patterns);

  $custom_patterns = $path . '/yaml_translation_patterns.yml';
  if (!empty($path) && file_exists($custom_patterns)) {
    _potx_load_yaml_translation_patterns($custom_patterns);
  }
}

/**
 * Load the list of YAML translation patterns from a file.
 *
 * The file is expected to be in the following format:
 * @code
 *   # A list of file matching patterns (as expected by fnmatch()).
 *   *.test_format.yml:
 *     # A sequence of translatable keys in the matched YAML files.
 *     - test_label
 *     - test_label_2:
 *         # An optional key, used as context for the translatable string.
 *         context: label_2_context
 * @endcode
 *
 * @param string $path
 *   The path for the YAML translation patterns file.
 */
function _potx_load_yaml_translation_patterns($path) {
  global $yaml_translation_patterns;

  $content = Yaml::parse(file_get_contents($path));

  foreach ($content as $pattern => $list) {
    foreach ($list as $value) {
@@ -1882,7 +1923,7 @@ function _potx_parse_yaml_file($code, $file_name, $save_callback) {
  global $yaml_translation_patterns;

  if (!isset($yaml_translation_patterns)) {
    _potx_init_translation_patterns();
    _potx_init_yaml_translation_patterns();
  }

  foreach ($yaml_translation_patterns as $pattern => $trans_list) {
@@ -1893,6 +1934,14 @@ function _potx_parse_yaml_file($code, $file_name, $save_callback) {
  }
}

/**
 * Recursively look for translatable strings in a YAML file.
 *
 * @param array $yaml
 *   The parsed YAML data.
 * @param array $trans_list
 *   The list of translatable keys to look for, and (optionally) their contexts.
 */
function _potx_find_yaml_translatables($yaml, $trans_list, $file_name, $save_callback) {
  foreach ($yaml as $key => $value) {

@@ -1995,6 +2044,7 @@ function _potx_explore_dir($path = '', $basename = '*', $api_version = POTX_API_
  if ($api_version > POTX_API_7) {
    $extensions[] = 'twig';
    $extensions[] = 'yml';
    _potx_init_yaml_translation_patterns($path);
  }
  $files = array();
  foreach ($extensions as $extension) {
+10 −0
Changes for tests/potx.test: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -337,6 +337,16 @@ class PotxTestCase extends DrupalWebTestCase {
    $this->assertMsgIDContext('Test menu link title with context', 'Menu item context');
  }

  /**
   * Test parsing of custom yaml files.
   */
  public function testDrupal8CustomYml() {
    _potx_init_yaml_translation_patterns(drupal_get_path('module', 'potx') . '/tests/potx_test_8/');
    $this->parseFile(drupal_get_path('module', 'potx') . '/tests/potx_test_8/potx_test_8.test.yml', POTX_API_8);
    $this->assertMsgID('Test custom yaml translatable');
    $this->assertMsgIDContext('Test custom yaml translatable with context', 'Yaml translatable context');
  }

  /**
   * Test parsing of Drupal 8 configuration schema files.
   */
+5 −0
Changes for tests/potx_test_8/potx_test_8.test.yml: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
name: 'Test custom yaml translatable'
test_data:
  test_key: 1
  test_label: 'Test custom yaml translatable with context'
  label_context: 'Yaml translatable context'
+5 −0
Changes for tests/potx_test_8/yaml_translation_patterns.yml: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
potx_test_8.*.yml:
  - name
  - test_label:
      context: label_context