bigBear713 / nb-trans

an angular(2+) component about i18n.
https://bigbear713.github.io/nb-trans/
MIT License
49 stars 6 forks source link

Can use Symbol replace the uuid lib? #23

Closed bigBear713 closed 1 year ago

bigBear713 commented 1 year ago

version: v15.1.0

We know in translated statement, if a prev param value contains the next param key, there is a problem when using the prev param value to replace the corresponding param key.

// translated statement
const transStr = 'this is {{p1}}, this is {{p2}}';

// param value
const params={
   p1:'{{p2}}',
  p2:'param value 2',
};

// replace param key '{{p1}}' as param value '{{p2}}'
const transStrTmp = 'this is {{p2}}, this is {{p2}}';

// replace param key '{{p2}}' as param value 'param value 2'
const wrongResult = 'this is param value 2, this is param value 2';

// what we want
const rightResult = 'this is {{p2}}, this is param value 2';

So now using uuid to replace all param key in translated statement firstly, then using param values to replace the param keys corresponding uuid value in statement. But so we have to import uuid lib. I think it is better if here can use Symbol to replace uuid, because the uuid lib can give us unique values, and the Symbol also can be unique

bigBear713 commented 1 year ago

It seems unnecessary to use uuid lib or Symbol. I put the data in params into the trans string before, so had to replce the param key with an unique value in trans string first, to prevent some param values are duplicated with some param keys. You know it will cause some error. So I import the uuid lib. Now I am going to change the way of handling. Before, the trans string is always a string, now, I am going to split it as a string array based on param keys. So can use the param keys in trans string array to get param values from params directly.

bigBear713 commented 1 year ago

Will use regexp to split the transled string, so in order to keep the content safe, we should limit the value of param key. It's a good idea to keep the naming rules consistent with JS variable names:

  1. Consists of letters, numbers, _, and $
  2. The number can't be the first character
bigBear713 commented 1 year ago

It is better if here can print the warning info when a param key is invalid. It can help the developers to fix the error.

Also, developers can turn off the print of the warning info.

It is better the print of the warning info will auto be turned off in prod env

bigBear713 commented 1 year ago

The uuid lib has been removed in v16.0.0