strongloop / strong-globalize

strong-globalize is built on Unicode CLDR and jquery/globalize and implements automatic extraction of strings from JS source code and HTML templates, lint the string resource, machine-translate them in seconds. In runtime, it loads locale and string resource into memory and provides a hook to persistent logging.
Other
25 stars 16 forks source link

Need to account for differences in accept-language header #157

Closed codechennerator closed 4 years ago

codechennerator commented 4 years ago

Issue: The changes I made last time fixed the issue for the regional accept-languages. However, it made a regression for headers without the accept-language header. While fixing the issue, I was thinking of other situations where accept-language inputs might be different. I also realized that there are weighted http headers. So that needs to be accounted for too.

No Accept language header With a test of

test('no accept-language header', function(t) {
  var req = {
    headers: {
      accept: 'application/json',
    },
  };
  var message = g.http(req).f('Test message');
  t.equal(message, 'Test message');
  t.end();
});

Results will return:

✖ Cannot read property 'split' of undefined

    '*** setRootDir: Root path is not a directory: ' + rootPath.toString()
  );
  let files: string[] | undefined = undefined;
  try {

  test: no accept-language header
  stack: |
    Object.getLanguageFromRequest (packages/runtime/src/helper.ts:69:5)
    StrongGlobalize.http (packages/runtime/lib/strong-globalize.js:19:1315)
    Test.<anonymous> (packages/runtime/test/test-load-msg.js:65:19)
  at:
    line: 69
    column: 5
    file: packages/runtime/src/helper.ts
    function: Object.getLanguageFromRequest
  type: TypeError
  tapCaught: testFunctionThrow

Weighted accept-language header

test('multiple, weighted, accept-language header', function(t) {
  var req = {
    headers: {
      'accept-language': 'en;q=0.3, zh-cn;q=0.7, zh-tw;q=1.0',
    },
  };
  var sg_hant = new SG({language: 'zh-Hant'});
  SG.sgCache.set('zh-Hant', sg_hant);

  var cachedSg = g.http(req);
  t.equal(cachedSg.getLanguage(), 'zh-Hant');

  var message = g.http(req).f('log');
  t.equal(message, '原木');
  t.end();
});

will return

 FAIL  packages/runtime/test/test-load-msg.js
 ✖ should be equal

  packages/runtime/test/test-load-msg.js               
  126 |                                                
  127 |   var cachedSg = g.http(req);                  
> 128 |   t.equal(cachedSg.getLanguage(), 'zh-Hant');  
      | ----^                                          
  129 |                                                
  130 |   var message = g.http(req).f('log');          
  131 |   t.equal(message, '原木');                      

  --- expected    
  +++ actual      
  @@ -1,1 +1,1 @@ 
  -zh-Hant        
  +en             
agnes512 commented 4 years ago

@jannyHou could you take case of the issue and the PR? thanks!

dhmlau commented 4 years ago

@codechennerator, your PR has landed. Is this issue good to close? Thanks.

codechennerator commented 4 years ago

Closing! Thanks!