---
# file: roles/drupal/tasks/reset-locale.yml

- block:

    - name: Set default facts
      set_fact:
        drushAlias: '@{{ drupal.id }}.{{ drupal_domain.shortname|default("default") }}'
        drushAliasValidate: "'@{{ drupal.id }}.{{ drupal_domain.shortname|default('default') }}':"
    - name: Set facts for Drush 8
      set_fact:
        drushAlias: '@{{ inventory_hostname }}{{ drushSubkey }}.{{ drupal_domain.shortname|default("default") }}'
        drushAliasValidate: '@{{ inventory_hostname }}{{ drushSubkey }}.{{ drupal_domain.shortname|default("default") }}'
      when: drush_version_main|default('8') == '8'

    - name: Reset locale
      shell: drush -y {{ drushAlias }} {{ item }}
      args:
        chdir: '{{ webRoot }}'
      with_items:
        - sql:query "truncate locales_location;"
        - sql:query "truncate locales_source;"
        - sql:query "truncate locales_target;"
        - sql:query "truncate locale_file;"
        - sql:query "delete from key_value where collection='locale.translation_status';"
        - sdel locale.translation_last_checked
        - locale:check
        - locale:update
      changed_when: no
      when: drushAliasValidate in drush_aliases.stdout_lines

    - name: Import extra locale files
      shell: drush -y {{ drushAlias }} locale:import {{ item.lang }} {{ item.file }}  --type=custom --override=all
      args:
        chdir: '{{ webRoot }}'
      with_items: '{{ drupal_domain.extra_locales|default([]) }}'
      changed_when: no
      when: drushAliasValidate in drush_aliases.stdout_lines

  tags:
    - reset-locale
