kibana/x-pack/plugins/canvas/i18n
Catherine Liu a4dd96a7b9
Revert "Revert "[Canvas] By-Value Embeddables (#113827)" (#116527)" (#117613) (#117751)
* Revert "Revert "[Canvas] By-Value Embeddables (#113827)" (#116527)"

This reverts commit 9e6e84571f.

* Fix ts error

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2021-11-10 13:06:43 -07:00
..
elements [Canvas] TagCloud (#106858) 2021-09-06 04:13:38 -04:00
functions Revert "Revert "[Canvas] By-Value Embeddables (#113827)" (#116527)" (#117613) (#117751) 2021-11-10 13:06:43 -07:00
templates Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
capabilities.ts Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
constants.ts [Canvas] Expression reveal image. Async libs and images loading. (#103399) 2021-07-19 17:10:18 +03:00
errors.ts [Canvas] Expression repeat image (#104255) 2021-07-23 11:29:06 +03:00
expression_types.ts Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
index.ts [canvas] Reduce bundle size by co-locating strings with components (#103013) 2021-06-23 17:04:19 -05:00
lib.ts Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
README.md Updates Github link references from master to main (#116789) (#116791) 2021-10-29 10:07:44 -07:00
renderers.ts [Canvas] Expression progress (#104457) 2021-08-04 11:33:01 +03:00
shortcuts.ts Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
tags.ts Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
transitions.ts Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
ui.ts [Canvas] TagCloud arguments. (#107729) 2021-09-17 07:34:15 -04:00
units.ts Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00

Canvas and Internationalization (i18n)

Creating i18n strings in Kibana requires use of the @kbn/i18n library. The following outlines the strategy for localizing strings in Canvas

Why i18n Dictionaries

In Canvas, we prefer to use "dictionaries" of i18n strings over including translation inline. There are a number of reasons for this.

API Signature is Lengthy

A call to localize a string can look something like this:

i18n.translate('xpack.canvas.functions.alterColumn.args.columnHelpText', {
  defaultMessage: 'The name of the column to alter.',
}),

But it can also look something like this:

i18n.translate('xpack.canvas.functions.alterColumnHelpText', {
  defaultMessage:
    'Converts between core types, including {list}, and {end}, and rename columns. ' +
    'See also {mapColumnFn} and {staticColumnFn}.',
  values: {
    list: Object.values(DATATABLE_COLUMN_TYPES)
      .slice(0, -1)
      .map(type => `\`${type}\``)
      .join(', '),
    end: Object.values(DATATABLE_COLUMN_TYPES).slice(-1)[0],
    mapColumnFn: '`mapColumn`',
    staticColumnFn: '`staticColumn`',
  },
});

In either case, including all of this code inline, where the string is ultimately utilized, makes the code look very uneven, or even complicated. By externalizing the construction of localized strings, we can reduce both of these examples:

import { FunctionStrings } from './some/i18n/dictionary';
const { AlterColumn: strings } = FunctionStrings;

const help = strings.getColumnHelpText();
const moreHelp = strings.getAlterColumnHelpText();

Reducing Duplication, Auditing

By externalizing our strings into these functional dictionaries, we also make identifying duplicate strings easier... thus removing workload from translation teams. We can also deprecate functions. And Since they're written in Typescript, finding usage is easier, so we can easily remove them if a string is no longer used.

It will also make writing more advanced auditing tools easier.

Creating i18n Dictionaries

There are some Best Practices™️ to follow when localizing Canvas strings:

  • Create dictionaries in /canvas/i18n.
    • Organize first by the top-level subject or directory, (e.g. functions, renderers, components, etc).
  • Don't create too many files. Prefer to eventually split up a dictionary rather than start with many small ones.
    • Let's avoid ten files with two strings apiece, for example.
  • Create functions that produce a string, rather than properties of strings.
    • Prefer getSomeString({...values}) => i18n.translate(...);.
    • Avoid someString: i18n.translate(...);.
    • Standardizes the practice, also allows for passing dynamic values to the localized string in the future.
    • Exception to this is the dictionary for Canvas Functions, which use a more complex dictionary, influenced by data/interpreter.

Constants

In some cases, there are proper nouns or other words that should not be translated, (e.g. 'Canvas', 'JavaScript', 'momentJS', etc). We've created /canvas/i18n/constants.ts to collect these words. Use and add to these constants as necessary.