Pete Bacon Darwin
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # $localize - message-id support ## Background The original [design doc](https://hackmd.io/UfFN_wyVT-Ob1p70nA3N_Q) for `$localize` avoids message-ids for Angular v9 since the template compiler does not need to generate messages based on ids. Therefore the original implementation for `$localize` relies upon the source-message as the "key" for looking up translations. For example give then following template: ```htmlmixed= <head> <div i18n> Hello, {{title}}! </div> </head> ``` The Angular compiler generates the following source-message, which is then used as a translation-key. ```typescript " Hello, {$interpolation}! " ``` ### Problems with source-message as translation-key #### Multiple meanings It is possible for there to be more than one translation for a source message depending upon the context, requiring a custom/manual id: ``` "right" (correct) => vrai "right" (opposite of left) => droit "right" (fair) => juste ``` In the curent version of Angular the message "meaning" is combined with the source-message to compute the id. #### Whitespace removal In order to support consistent rendering of expandable ICU messages (containing markup), the source-message has its whitespace removed, if `preserveWhitespace: false`. This means that the source-message in the template is different to that extracted into translation files. In the example given in [Background](#background) the source-message in the template is: ```typescript " Hello, {$interpolation}! " ``` while in the translation file it is: ```typescript "\n Hello, {$interpolation}!\n " ``` This prevents accurate translation lookup based on the source-message string. ## Proposal ### Localized string syntax Extend the format of localized strings to allow "meaning", "description" and "message-id" to be provided. This metadata is provided at the start of the string marked by colons. Each of these is optional. For example: ```typescript $localize`:meaning|description@@message-id:source message text`; $localize`:meaning|:source message text`; $localize`:description:source message text`; $localize`:@@message-id:source message text`; ``` :::info The delimiters within the colons are chosen to match those already used in Angular templates. For example:<br><br> ```htmlmixed <h1 i18n="site header|An introduction header@@introductionHeader"> Hello i18n! </h1> ``` ::: :::info If a source-message actually starts with a colon then it must be escaped with a backslash. For example:<br><br> ```typescript $localize`\:this message actually starts with a colon (:)`; ``` This approach is similar to that already implemented for named placeholders: the substitution is post-fixed by a colon delimited placeholder name. For example:<br><br> ```typescript $localize`Hello ${item.name}:title:`; // placeholder name is 'title' $localize`${hours}\:${mins}\:${secs}`; // colons are part of the message ``` ::: ### Template generation The template source-message strings are not guaranteed to be identical to those in translation files. See [Whitespace removal](#Whitespace-removal)) above. Therefore we must provide the computed id when generating `$localize` tagged strings in template generation. For example: ```typescript $localize `:@@123456789: Hello, {$interpolation}! `; ``` This ensures that translation of this message is based on the message-id and not the source-message. ### Translation matching Use the message-id as the key when looking up translations rather than the source-message. If a source-message does not provide a custom message-id then compute one. Computed message-ids are generated using the same algorithm as XLIFF2 and XMB/XTB formats. This is implemented in the `decimalDigest()` function. ### $localize.translate() implementation The `$localize()` function passes the `messageParts` and `expressions` to the `$localize.translate()` run-time translation function. The current implementation computes the source-message and uses that as the translation-key. ```typescript function translate(messageParts: TemplateStringsArray, expressions: readonly any[]): [TemplateStringsArray, readonly any[]]; ``` Modify this function to compute the message-id instead and use that as the translation-key. ### Translation storage Internally each translation is stored as a `ParsedTranslation` object, which is kept in a lookup table. ```typescript export interface ParsedTranslation { messageParts: TemplateStringsArray; placeholderNames: string[]; } type SourceMessage = string; type InternalTranslations = Record<SourceMessage, ParsedTranslation>; ``` Modify this lookup table so that the key is the message-id. ```typescript type MessageId = string; type InternalTranslations = Record<MessageId, ParsedTranslation>; ``` ### Loading translations The `loadTranslations()` function accepts a `translations` argument: ```typescript export type Translations = Record<SourceMessage, TargetMessage>; export function loadTranslations(translations: Translations): void; ``` Change this function to accept a `translations` argument that is a map of message-ids instead of source-messages: ```typescript export type Translations = Record<MessageId, TargetMessage>; export function loadTranslations(translations: Translations): void; ``` :::warning When calling `loadTranslations()`, the caller is now responsible for providing the message-id of each translation: either custom message-ids or computed via `decimalDigest()`. ::: If the translations are loaded from a formatted translation file that uses the same algorithm as `decimalDigest()`, e.g. XLIFF2 or XTB, then the loader can just use the message-ids directly. If the translations are loaded from a formatted translation file uses a different digest algorithm, e.g. XLIFF 1.2, then the loader must convert the given message-id before calling `loadTranslations()`. This is done as follows: * Compute the message-id, using the appropriate digest algorithm, from the source-message in the file * Check this computed message-id against the one stored in the translation file. * If the computed message-id is different to the one in the file, then this must be a custom message-id * use the custom message-id unchanged * else the message-ids are identical then this must be a computed message-id so compute a new message-id the `decimalDigest()` algorithm. * use the new computed message-id

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully