Skip to content

Commit

Permalink
Parse references from comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko committed Sep 5, 2019
1 parent 45eed35 commit f117658
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
25 changes: 21 additions & 4 deletions client/app/services/translation-utils.service.js
Expand Up @@ -98,6 +98,9 @@ export default class TranslationUtils {
if(original._context) {
newProcessed._context = original._context;
}
if(original._references) {
newProcessed._references = original._references;
}

if(!this.isInnermostParsedObject(original)) {
buildNewDataRecursive(newProcessed, original, translated, processed);
Expand Down Expand Up @@ -226,8 +229,12 @@ export default class TranslationUtils {
/**
* Analyzes a YAML document and extracts informations included in commented lines.
*
* Comments starting with `context:` above a key are saved in the _context property
* in the corresponding object.
* Comments starting with `context:` above a key are saved
* in the _context property in the corresponding object.
* Comments starting with `original_hash:` above a key are saved
* in the _originalHash property corresponding object.
* Comments starting with `reference:` above a key are saved
* in the _references property corresponding object (an array that may contain multiple references).
*
* @param {String} text A YAML document.
* @param {Object} data A parsed data corresponding to the YAML document.
Expand Down Expand Up @@ -256,6 +263,10 @@ export default class TranslationUtils {
if(originalHash) {
data[key]._originalHash = originalHash;
}
let references = this.referencesFromComments(commentLines);
if(references.length > 0) {
data[key]._references = references;
}
return beginning;
});
}
Expand All @@ -276,6 +287,12 @@ export default class TranslationUtils {
return line && line.replace('original_hash: ', '');
}

referencesFromComments(commentLines) {
return commentLines
.filter(line => line.startsWith('reference: '))
.map(line => line.replace('reference: ', '').trim());
}

/**
* Adds comments starting with `original_hash:` above each innermost key.
* They include 7 first characters of SHA1-hashed original texts.
Expand Down Expand Up @@ -405,7 +422,7 @@ export default class TranslationUtils {
* A generator function that iterates over the given data
* and yields only those keys that haven't been translated yet.
*
* Yields objects with two properties:
* Yields objects with three properties:
* - path (dot separated key names hierarchy, e.g. en.common.day)
* - value (an actual translation of the key)
* - chain (an array with the hierarchy, consists of objects of the form { key, data })
Expand Down Expand Up @@ -484,7 +501,7 @@ export default class TranslationUtils {
}

isPrivateKey(key) {
return ['_context'].includes(key);
return ['_context', '_references'].includes(key);
}

isTranslated(processedObject) {
Expand Down
24 changes: 24 additions & 0 deletions spec/angular-unit/services/translation-utils.service.spec.js
Expand Up @@ -295,6 +295,30 @@ describe('TranslationUtils', () => {
expect(data.common.colors.black._context).toEqual('A comment about the black color.');
expect(data.colors._context).toEqual('Just a name for a group of colors.');
});

it('handles reference comments', () => {
let yaml = `
en:
common:
colors:
white: white
black: black
#reference: common.colors.white
#reference: common.colors.black
colors: colors like white and black
`;
let data = {
common: {
colors: {
white: { _value: 'white' },
black: { _value: 'black' }
}
},
colors: { _value: 'colors' }
};
TranslationUtils.parseComments(yaml, data);
expect(data.colors._references).toEqual(['common.colors.white', 'common.colors.black']);
});
});

describe('addHashes', () => {
Expand Down

0 comments on commit f117658

Please sign in to comment.