You are here

function _potx_parse_yaml_file in Translation template extractor 7.3

Same name and namespace in other branches
  1. 8 potx.inc \_potx_parse_yaml_file()
  2. 6.3 potx.inc \_potx_parse_yaml_file()
  3. 7.2 potx.inc \_potx_parse_yaml_file()

Parse a YAML file for translatables. Drupal 8+.

1 call to _potx_parse_yaml_file()
_potx_process_file in ./potx.inc
Process a file and put extracted information to the given parameters.

File

./potx.inc, line 2101
Extraction API used by the web and command line interface.

Code

function _potx_parse_yaml_file($code, $file_name, $file_path, $save_callback) {
  global $yaml_translation_patterns;
  global $potx_callbacks;
  global $_potx_module_metadata;
  if (!is_array($yaml_translation_patterns)) {
    _potx_init_yaml_translation_patterns();
  }
  try {
    $yaml = Yaml::parse($code);

    // Don't try to process empty yaml files.
    if (!is_array($yaml)) {
      return;
    }
  } catch (ParseException $e) {
    potx_status('error', t("YAML parsing error on file @path: @error", array(
      '@path' => $file_path,
      '@error' => $e
        ->getMessage(),
    )), $file_path);
    return;
  }
  foreach ($yaml_translation_patterns as $pattern => $trans_list) {
    if (fnmatch($pattern, $file_name) || fnmatch('*/' . $pattern, $file_name)) {
      _potx_find_yaml_translatables($yaml, $trans_list, $file_name, $save_callback, TRUE);
    }
  }
  if (preg_match('~config/(schema|install|optional)/[^/]+\\.yml$~', $file_name, $matches)) {
    $module_name = basename(dirname(dirname(dirname($file_name))));
    $_potx_module_metadata[$module_name]['config'][$matches[1]][] = array(
      $file_name,
      $file_path,
    );
    if ($matches[1] == 'schema') {
      $keys = array_keys($yaml);
      $potx_callbacks['schema_store_lookup']($keys, $module_name);
    }
  }
  elseif (preg_match('~[^/]+\\.info\\.yml~', $file_name)) {
    $module_name = basename(dirname($file_name));
    $_potx_module_metadata[$module_name]['dependencies'] = [];

    // Module and theme projects have dependencies. Profiles have modules
    // listed in the install key.
    if (isset($yaml['dependencies'])) {
      $_potx_module_metadata[$module_name]['dependencies'] = $yaml['dependencies'];
    }
    if (isset($yaml['install'])) {
      $_potx_module_metadata[$module_name]['dependencies'] = $yaml['install'];
    }

    // Remove composer style naming prefix for dependencies.
    foreach ($_potx_module_metadata[$module_name]['dependencies'] as &$dependency) {
      $dependency = preg_replace('!^.+:(\\S+)!', '\\1', $dependency);
    }
  }
}