Skip to content

Help! This doesn't translate!

UnknownJoe796 edited this page Sep 6, 2022 · 1 revision

The translation system in Khrysalis is extremely flexible and built for adding new translations dynamically.

Simple Example

Let's say we need support for URL encoding - specifically, calling the function URLEncoder.encode("string") in Kotlin.

Add a file in your-project/src/main/equivalents called your-name-here.swift.yaml.

- id: java.net.URLEncoder.encode
  type: call
  template: "~0~.addingPercentEncoding(withAllowedCharacters: .alphanumerics)"

And likewise for TypeScript, add a file in your-project/src/main/equivalents called your-name-here.ts.yaml.

- id: java.net.URLEncoder.encode
  type: call
  template: encodeURIComponent(~0~)

Examples requiring Imports

What about if we need something from outside the standard libraries? Let's pull an example from the built-in equivalents, specifically a RatingBar's number of stars on iOS:

# Swift
- id: android.widget.RatingBar.numStars
  type: get
  template:
    pattern: ~this~.settings.totalStars
    imports: [Cosmos]  # import Cosmos

Or how about generating a random UUID in TypeScript?

# Typescript
- id: java.util.UUID.randomUUID
  type: call
  template:
    pattern: v4()
    imports:
      v4: uuid  # import { v4 } from 'uuid'

Types of Equivalents

  • call - A call to a function.
  • get - The reading of a variable.
  • set - The writing of a variable.
  • type - A type (e.g. Int becomes number in TypeScript
  • typeRef - A reference to the class of a type (e.g. Int::class becomes Number in TypeScript)

More Examples

The vast majority of the built-in equivalents are done in this same YAML format. You can find those here:

More information can be found in the equivalents documentation.