Polyconseil / easygettext

Simple gettext tokens extraction tools for HTML and Jade files.
79 stars 52 forks source link

gettext-extract is not extracting text from vue files. #41

Closed uroybd closed 6 years ago

uroybd commented 6 years ago

Here's the relevent codes.

First, a Component:

<template>
  <div class="hello">
    <h1>{{msg}}</h1>
    <span v-translate>Hello!</span>
    <translate>Hello!</translate>
  </div>
</template>

<script>
 export default {
   name: 'HelloWorld',
   data () {
     return {
       msg: 'Welcome to Your Vue.js App'
     }
   }
 }
</script>

Here's the Makefile modified per my project requirement:

# On OSX the PATH variable isn't exported unless "SHELL" is also set, see: http://stackoverflow.com/a/25506676
SHELL = /bin/ash
NODE_BINDIR = /app/node_modules/.bin
export PATH := $(NODE_BINDIR):$(PATH)

# Where to write the files generated by this makefile.
OUTPUT_DIR = /app/server/assets/translation
SOURCE_DIR = /app/server/assets/js

# Available locales for the app.
LOCALES = en_US bn_BD

# Name of the generated .po files for each available locale.
LOCALE_FILES ?= $(patsubst %,$(OUTPUT_DIR)/locale/%/LC_MESSAGES/app.po,$(LOCALES))

GETTEXT_HTML_SOURCES = $(shell find $(SOURCE_DIR) -name '*.vue' -o -name '*.html' 2> /dev/null)
GETTEXT_JS_SOURCES = $(shell find $(SOURCE_DIR) -name '*.vue' -o -name '*.js')

# Makefile Targets
.PHONY: clean makemessages translations

clean:
    rm -f /tmp/template.pot $(OUTPUT_DIR)/translations.json

makemessages: /tmp/template.pot

translations: $(OUTPUT_DIR)/translations.json

# Create a main .pot template, then generate .po files for each available language.
# Thanx to Systematic: https://github.com/Polyconseil/systematic/blob/866d5a/mk/main.mk#L167-L183
/tmp/template.pot: $(GETTEXT_HTML_SOURCES)
# `dir` is a Makefile built-in expansion function which extracts the directory-part of `$@`.
# `$@` is a Makefile automatic variable: the file name of the target of the rule.
# => `mkdir -p /tmp/`
    mkdir -p $(dir $@)
    which gettext-extract
# Extract gettext strings from templates files and create a POT dictionary template.
    gettext-extract --attribute v-translate --quiet --output $@ $(GETTEXT_HTML_SOURCES)
# Extract gettext strings from JavaScript files.
    xgettext --language=JavaScript --keyword=npgettext:1c,2,3 \
        --from-code=utf-8 --join-existing --no-wrap \
        --package-name=logolagbe \
        --package-version=1.0.0 \
        --output $@ $(GETTEXT_JS_SOURCES)
# Generate .po files for each available language.
    @for lang in $(LOCALES); do \
        export PO_FILE=$(OUTPUT_DIR)/locale/$$lang/LC_MESSAGES/app.po; \
        echo "msgmerge --update $$PO_FILE $@"; \
        mkdir -p $$(dirname $$PO_FILE); touch $$PO_FILE \
        [ -f $$PO_FILE ] && msgmerge --lang=$$lang --update $$PO_FILE $@ || msginit --no-translator --locale=$$lang --input=$@ --output-file=$$PO_FILE; \
        msgattrib --no-wrap --no-obsolete -o $$PO_FILE $$PO_FILE; \
    done;

$(OUTPUT_DIR)/translations.json: clean /tmp/template.pot
    mkdir -p $(OUTPUT_DIR)
    gettext-compile --output $@ $(LOCALE_FILES)

And here's a .gettext.json because it was complaining that there is no configuration:

{
  "js": {
    "parsers": [
      {
        "expression": "gettext",
        "arguments": {
          "text": 0
        }
      },
      {
        "expression": "ngettext",
        "arguments": {
          "text": 0,
          "textPlural": 1
        }
      },
      {
        "expression": "pgettext",
        "arguments": {
          "context": 0,
          "text": 1
        }
      }
    ],
    "glob": {
      "pattern": "src/**/*.ts",
      "options": {
        "ignore": "src/**/*.spec.ts"
      }
    }
  },
  "html": {
    "parsers": [
      {
        "element": "[translate]",
        "attributes": {
          "textPlural": "translate-plural",
          "context": "translate-context"
        }
      },
      {
        "attribute": "translate-text",
        "attributes": {
          "textPlural": "translate-plural",
          "context": "translate-context"
        }
      }
    ],
    "glob": {
      "pattern": "src/**/*.html"
    }
  },
  "headers": {
    "Language": ""
  },
  "output": "translations/template.pot"
}

But it is not compiling. Showing this error:

/app/server # make makemessages
mkdir -p /tmp/
which gettext-extract
/app/node_modules/.bin/gettext-extract
gettext-extract --attribute v-translate --quiet --output /tmp/template.pot /app/server/assets/js/App.vue /app/server/assets/js/components/FooterComponent.vue /app/server/assets/js/components/HomeAnimation.vue /app/server/assets/js/components/LoginForm.vue /app/server/assets/js/components/test.html /app/server/assets/js/components/HelloWorld.vue /app/server/assets/js/components/Bla.vue /app/server/assets/js/components/HeaderComponent.vue

  0 messages extracted
  --------------------------
  0 total usages
  0 files (0 with messages)
  0 message contexts

xgettext --language=JavaScript --keyword=npgettext:1c,2,3 \
        --from-code=utf-8 --join-existing --no-wrap \
        --package-name=logolagbe \
        --package-version=1.0.0 \
        --output /tmp/template.pot /app/server/assets/js/store/actions.type.js /app/server/assets/js/store/auth.module.js /app/server/assets/js/store/mutations.type.js /app/server/assets/js/store/index.js /app/server/assets/js/App.vue /app/server/assets/js/router/index.js /app/server/assets/js/common/token.service.js /app/server/assets/js/common/config.js /app/server/assets/js/common/api.service.js /app/server/assets/js/components/FooterComponent.vue /app/server/assets/js/components/HomeAnimation.vue /app/server/assets/js/components/LoginForm.vue /app/server/assets/js/components/HelloWorld.vue /app/server/assets/js/components/Bla.vue /app/server/assets/js/components/HeaderComponent.vue /app/server/assets/js/index.js
xgettext: error while opening "/tmp/template.pot" for reading: No such file or directory
make: *** [Makefile:38: /tmp/template.pot] Error 1
sebdiem commented 6 years ago

Hello,

Given the error that you get:

xgettext: error while opening "/tmp/template.pot" for reading: No such file or directory

it does not seem directly related to easygettext. Does the /tmp/ directory exist on your machine ?

uroybd commented 6 years ago

Yes, it's a docker container in alpine, it was there. Whatever, I will check again in ubuntu. And will let you know.

On Thu, Aug 2, 2018, 7:32 PM Sébastien Diemer notifications@github.com wrote:

Hello,

Given the error that you get:

xgettext: error while opening "/tmp/template.pot" for reading: No such file or directory

it does not seem directly related to easygettext. Does the /tmp/ directory exist on your machine ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Polyconseil/easygettext/issues/41#issuecomment-409927279, or mute the thread https://github.com/notifications/unsubscribe-auth/AEAyfiZguoVAGC0G1gcfwdsBKXynbV7Hks5uMv94gaJpZM4VWFDP .

-- উৎসব রায় (Utsob Roy) https://utsob.me