From 4294adbc682ed2ae03c0869722ed62efb58670e9 Mon Sep 17 00:00:00 2001 From: Pat O'Callaghan Date: Fri, 22 Jul 2022 21:09:01 +0100 Subject: [PATCH] fix: key name when translation file is at the root (#392) Follow up to the bug found in https://github.com/lifeart/ember-language-server/pull/389#issuecomment-1192553091 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` --- src/builtin-addons/core/intl-utils.ts | 4 +- .../bultin-addons/core/intl-providers-test.ts | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/builtin-addons/core/intl-utils.ts b/src/builtin-addons/core/intl-utils.ts index 40108cc9..735b623e 100644 --- a/src/builtin-addons/core/intl-utils.ts +++ b/src/builtin-addons/core/intl-utils.ts @@ -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) { diff --git a/test/bultin-addons/core/intl-providers-test.ts b/test/bultin-addons/core/intl-providers-test.ts index 2fb3ebd1..ffffe241 100644 --- a/test/bultin-addons/core/intl-providers-test.ts +++ b/test/bultin-addons/core/intl-providers-test.ts @@ -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( (