Skip to content

Templates for Internalization (messages, formatting) #138

@jozef-slezak

Description

@jozef-slezak

Goal: Refactor existing internalization code from ts.factory*() to templates

Background: we believe it is more intuitive to extend and maintain the codegen functionality using templates (rather then typescript factories). Typescript factory code builds AST using code but it tends to be hard to read once you get to bigger code snippet. We have tried different approach: we parse source file (template) as an AST and then apply refactoring rules like here https://github.com/iteria-app/lowcode/blob/master/packages/react-lowcode/src/codegen/generation/generators/template/template-resolver.ts

How to do that?

  • keep existing interfaces / API
  • introduce new implementation of the API using templates + refactoring rules
  • check that existing jest test are still green (after switching implementation)

The template source file:

  1. template should be normal normal working code
  2. refactoring rules should inline function from the template to the target/generated code
  3. template should look like this:

different templates for: react-intl or standard browser Intl:

export formatNumberPlaceholder() {
   return usage of react-intl format
}

export formatDatePlaceholder(todo params) {
   return usage of react-intl format
}

export formatTimePlaceholder(todo params) {
   return usage of react-intl format
}

Different template for react-intl or react-i18n

//react-intl.ts
export function T(messageID, defaultMessage) {
   const intl = useIntl()
   return intl.formatMessage(messageID, defaultMessage)
}

//similarly react-i18n.ts


// dummy-intl.ts
// this function will be replaced by `intl.`
export function T(messageID, defaultMessage) {
  return defaultMessage || messageID // default dummy implementation (without any framework )
}

export TranslatePlaceholder // TODO react tag code, params messageID
   return <FormattedMessage ... tag="span" />
}

What to do?

Checklist:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions