Skip to content

Commit

Permalink
fix: key name when translation file is at the root (#392)
Browse files Browse the repository at this point in the history
Follow up to the bug found in #389 (comment)

Make sure to generate the correct key name when the translation file as at the root translations directory, e.g. `translations/en-us.yml`.

### Before
`.var.folders.mk.5t937px5627_y_j551f8gbkm0000gn.T.tmp-84101gxBh16jwQL7q.translations.admin.countries`

### After
`admin.countries`
  • Loading branch information
patocallaghan authored Jul 22, 2022
1 parent 5c1ea9e commit 4294adb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/builtin-addons/core/intl-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ function generateTranslationKey(key: string, filePath: string, root: string): st

const dirname = path
.dirname(filePath)
.replace(new RegExp(`.*\\${path.sep}translations\\${path.sep}`), '')
.replace(new RegExp(`.*\\${path.sep}translations(\\${path.sep})?`), '')
.replace(new RegExp(`\\${path.sep}`, 'g'), '.');

return `${dirname}.${key}`;
return dirname.length ? `${dirname}.${key}` : key;
}

function addToHashMap(hash: TranslationsHashMap, translationFile: TranslationFile, locale: string, filePath: string, root: string) {
Expand Down
46 changes: 46 additions & 0 deletions test/bultin-addons/core/intl-providers-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,52 @@ for (const asyncFsEnabled of testCaseAsyncFsOptions) {
]);
});

it('should autocomplete when the translation is in the root file in handlebars', async () => {
expect(
(
await getResult(
CompletionRequest.method,
connection,
{
app: {
components: {
'test.hbs': `{{t "admin." }}`,
},
},
config: {
'ember-intl.js': `module.exports = function() { return { wrapTranslationsWithNamespace: true } }`,
},
translations: {
'en.yml': `admin:
foo: Bar`,
},
},
'app/components/test.hbs',
{ line: 0, character: 12 }
)
).response
).toEqual([
{
documentation: 'en : Bar',
kind: 12,
label: 'admin.foo',
textEdit: {
newText: 'admin.foo',
range: {
end: {
character: 7,
line: 0,
},
start: {
character: 7,
line: 0,
},
},
},
},
]);
});

it('should autocomplete in JS files when in the end of expression', async () => {
expect(
(
Expand Down

0 comments on commit 4294adb

Please # to comment.