L20n.js - how to access localization sub-objects from javascript

In this example, I see how to access variables inside the LOL resource

<brandName { nominative: "Firefox", genitive: "Firefoksa", dative: "Firefoksowi", accusative: "Firefoksa", instrumental: "Firefoksem", locative: "Firefoksie" }> <about "O {{ brandName.locative }}"> 

However, I also want to be able to do something similar in javascript, for example:

 document.l10.get('brandName.nominative'); 

This does not work for me. I debugged a bit, but it seems to me that I'm wasting time, and maybe this is an easy way to do this already. There is?

+4
source share
1 answer

This is not possible in L20n, because as a developer you cannot know exactly what nominative will be defined in all languages. Read on to find out why.

In English, your example might look like this:

 <brandName "Firefox"> <about "About {{ brandName }}"> 

This time no .nominative request!

L20n is an asymmetry. One version of a string in English can correspond to 4 variants in German, 7 in Polish and 16 in Hungarian. There is a social contract between the developer and L20n: if the developer asks for the value of an object named brandName , L20n will return a string that can be used in the user interface. All the work done to get this line is hidden from the developer and may vary depending on the language and logic that the localizers built.

Depending on what you want to achieve, you may find the following two constructs useful:

  • default values can be used to indicate the default key returned if you are not prompting for a specific one. In the example from your question, you can define nominative as the default value by putting an asterisk next to it, for example:

     <brandName { *nominative: "Firefox", genitive: "Firefoksa", dative: "Firefoksowi", accusative: "Firefoksa", instrumental: "Firefoksem", locative: "Firefoksie" }> <about "O {{ brandName.locative }}"> 

    When you try to get the brandName translation in JavaScript, you get the nominative key value, "Firefox":

     document.l10.get('brandName'); // "Firefox" 
  • attributes can be added to objects for storing metadata, for example:

     <brandName "Firefox" accesskey: "F" > 

    Note that attributes are different from dictionary values. In the above example, the value of brandName is a simple "Firefox" string. The brandName object has an optional accesskey attribute whose value is "F". You can access attributes using getEntity :

     document.l10.getEntity('brandName'); // { value: "Firefox", attributes: { accesskey: "F" } } 
+4
source

Source: https://habr.com/ru/post/1496989/


All Articles