Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

TypeScript #48

Closed
googol7 opened this issue Apr 28, 2016 · 7 comments
Closed

TypeScript #48

googol7 opened this issue Apr 28, 2016 · 7 comments

Comments

@googol7
Copy link

googol7 commented Apr 28, 2016

It would be very nice, if the tool also worked together with TypeScript.

@ericf
Copy link
Collaborator

ericf commented May 1, 2016

Please say more…

@googol7
Copy link
Author

googol7 commented May 2, 2016

In formatjs/formatjs#162 you said:

I only have plans to officially support the Babel plugin for extracting the default messages from source so they can be prepared for translation. It looks like you could have the TypeScript compiler output ES6 via the --target ES6 option, and then you could use Babel to transpile to ES3/5. It would be adding to your toolchain, but could open up your code base to more static analysis and tooling outside of what TypeScript provides.

It would be very nice though if "typescript-plugin-react-intl" could directly extract messages from TypeScript *.ts and *.tsx files.

@ericf
Copy link
Collaborator

ericf commented May 2, 2016

Okay, but that's not something I will be writing.

@ericf ericf closed this as completed May 2, 2016
@ericf ericf removed the needs info label May 2, 2016
@googol7
Copy link
Author

googol7 commented May 20, 2016

at the moment my script makemessages.sh looks like this:

#!/bin/bash
rm -rf trans-tmp extracted-messages
node_modules/.bin/tsc -p . --target ES6 --module es6 --jsx preserve --outDir trans-tmp
node_modules/.bin/babel --plugins react-intl "trans-tmp/**/*.jsx"

@davidjsalazarmoreno
Copy link

Thank you @googol7, your script works for me.

@googol7
Copy link
Author

googol7 commented Oct 18, 2016

I’m also using react-intl-translations-manager now. So my script makemessages.sh now looks like this:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import subprocess

commands = [
    "rm -rf src/app/l10n.bak",
    "cp -r src/app/l10n src/app/l10n.bak",
    "rm -rf trans-tmp extracted-messages",
    "node_modules/.bin/tsc -p . --target ES6 --module es6 --jsx preserve --outDir trans-tmp",
    "node_modules/.bin/babel --plugins react-intl \"trans-tmp/**/*.jsx\"",
]

progress = ""

for command in commands:
    progress += "."
    sys.stdout.write('%s\r' % (progress)),
    sys.stdout.flush()

    proc = subprocess.Popen([ command ], stdout=subprocess.PIPE, shell=True, stderr=subprocess.STDOUT,)
    (out, err) = proc.communicate()

    if "SyntaxError:" in out:
        print "SyntaxError occured. Stopping operation.\n"
        print out
        sys.exit(1)
        break

subprocess.Popen([ "npm run manage:translations" ], shell=True).wait()

sys.exit(0)

package.json contains:

  "scripts": {
    "manage:translations": "node_modules/.bin/babel-node ./translationRunner.js"
  },

translationRunner.js looks like this:

import manageTranslations, { readMessageFiles, createSingleMessagesFile } from "react-intl-translations-manager"

const messagesDir = "extracted-messages"
const translationsDir = "src/app/l10n/"
const singleMessageFileDir = "."

manageTranslations({
    messagesDirectory: messagesDir,
    translationsDirectory: translationsDir,
    languages: ["en", "cs", "de", "es", "fr", "it", "ja"],
})

const extractedMessages = readMessageFiles(messagesDir)

createSingleMessagesFile({
    messages: extractedMessages,
    directory: singleMessageFileDir,
    jsonSpaceIndentation: 4,
})

@krzkaczor
Copy link

You can use babel@7 to extract ids from typescript codebase without tsc compilation step 🎉
https://github.com/Neufund/platform-frontend/pull/534/files#diff-e56633f72ecc521128b3db6586074d2cR1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants