question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In a JSON file, allow references to other keys

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[ x ] feature request

Current behavior

Today, we can only set strings and variables like {{myVar}}. It would be great to be able to include other existing keys (like a partial translation).

Expected/desired behavior

{
   "greetings":"Hi",
   "text":"[greetings] from Github!",
   "text2":"[greetings] there!"
}
myService.get('text').subscribe(
    (value) => {
         console.log(value);
         // Hi, from Github!
    }
);

What is the motivation / use case for changing the behavior?

Today, we have to concatenate the translations on our side:

myService.get(['greetings', 'text']).subscribe(
    (translations) => {
         console.log(translations.greetings + ' ' +  translations.text);
    }
);

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:16
  • Comments:7

github_iconTop GitHub Comments

31reactions
SamVerschuerencommented, Mar 13, 2017

Don’t see any valid reason to do that. Just repeat the Hi and make it inline. This would just add complexity to the library without a real benefit imo.

3reactions
pfeiglcommented, Jan 24, 2018

This can be easily implemented via a TranslateCompiler (see ngx-translate documentation on how to register it). We are using this simple implementation:

export class TranslationCompiler extends TranslateCompiler {
    public compile(value: string, lang: string): string {
        return value;
    }

    public compileTranslations(translations: any, lang: string) {
        for (const key in translations) {
            if (translations.hasOwnProperty(key)) {
                translations[key] = this.resolveReferences(translations[key], translations);
            }
        }
        return translations;
    }

    private resolveReferences(value: string, translations: any) {
        return value.replace(/@:(\S+)/, (matches, key) => this.resolveReferences(translations[key], translations));
    }
}

Please be aware of the following problems you might face with this very simplistic implementation:

  • The compiler does support recursion, but does not check for infinite loops (aka back-and-forth references)
  • The compiler does not support nested translations
  • The compiler does not work with the compile method, as at this point the translations are no longer known (would require to keep track of all translations based on their language and merge them if multiple setTranslation calls for the same translation would be made)
  • Because of obvious syntax restrictions, the compiler can only reference keys which do not contain whitespace-characters
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use JSON references ($refs) - Redocly
OpenAPI allows for using JSON Reference objects. JSON References are required to accomplish: a multi-file structure; re-use of schemas or ...
Read more >
JSON Reference — JSON Spec documentation - Read the Docs
JSON Reference allows a JSON value to reference another value in a JSON document. This module implements utilities for exploring these objects.
Read more >
Accessing a JSON value that references another JSONs key
My problems start when I try to make the doing object as a JSON file as it requires the value sprite to be...
Read more >
Working with JSON - Learn web development | MDN
Other notes · JSON is purely a string with a specified data format — it contains only properties, no methods. · JSON requires...
Read more >
JSON Syntax - W3Schools
JSON names require double quotes. JSON - Evaluates to JavaScript Objects. The JSON format is almost identical to JavaScript objects. In JSON, keys...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found