Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: 🧪 Support folder namespaces for intl #389

Conversation

achambers
Copy link

@achambers achambers commented Jun 9, 2022

Fixes #344

Ember Intl exposes a configuration file which allows you to configure the localization setup. One such setting is wrapTranslationsWithNamespace which determines whether translation keys should have the folder names within the key names.

wrapTranslationsWithNamespace: false (default)

# translations/sub-folder/en-us.yml
subFolder:
  foo: bar
this.intl.t('subFolder.foo');

wrapTranslationsWithNamespace: true

# translations/sub-folder/en-us.yml
subFolder:
  foo: bar
this.intl.t('sub-folder.subFolder.foo');

wrapTranslationsWithNamespace: true is not currently supported by this extension. This PR adds the ability, when wrapTranslationsWithNamespace: true is set in app/config/ember-intl.js, to support keys which have folders in their key names.

@codecov
Copy link

codecov bot commented Jun 9, 2022

Codecov Report

Merging #389 (d1e2554) into component-context-info-origin (4618d27) will increase coverage by 0.09%.
The diff coverage is 96.42%.

@@                        Coverage Diff                        @@
##           component-context-info-origin     #389      +/-   ##
=================================================================
+ Coverage                          72.63%   72.72%   +0.09%     
=================================================================
  Files                                 57       57              
  Lines                               4915     4935      +20     
  Branches                             959      966       +7     
=================================================================
+ Hits                                3570     3589      +19     
- Misses                              1345     1346       +1     
Impacted Files Coverage Δ
src/builtin-addons/core/intl-utils.ts 95.90% <96.42%> (-0.18%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c2def00...d1e2554. Read the comment docs.

@lifeart
Copy link
Owner

lifeart commented Jul 14, 2022

Hi @achambers! Do you need any help with PR?

@patocallaghan
Copy link

patocallaghan commented Jul 15, 2022

@lifeart 👋 I'm taking over this PR from @achambers as he is on vacation atm and we're both on the same team 😃 I've just pushed an update there which tries to follow some of your advice from #344 (comment). Could you have a look and see if I'm on the right track?

With regards testing this out I'm having a bit of difficulty. I'm struggling to manually verify my changes are working because I can't seem to make my VSCode see my local version of ELS rather than my installed one?

Also with regards actual tests writing unit tests might be tricky due to the reliance on needing the ember-intl.js config file. I added some fixtures which may be a better way to verify this change but I've yet to write a test yet. thoughts?

@lifeart
Copy link
Owner

lifeart commented Jul 15, 2022

Hey @patocallaghan! Direction is good!

According to tests, I think having few related unit tests should be fine:
Samples:

Also with regards actual tests writing unit tests might be tricky due to the reliance on needing the ember-intl.js config file. - we can use virtual fs provider (from Server class) to mock it with our own, without having it in files

here you could specify project file structure as 3rd argument (including intl config) - https://github.com/lifeart/ember-language-server/blob/component-context-info-origin/test/bultin-addons/core/intl-providers-test.ts#L73 to verify results

it's key-value structure where key is a path to file, and value is file content:

 await getResult(
              CompletionRequest.method,
              connection,
              {
               'app/components/test.hbs': '',
               'config/ember-intl.js': `module.exports = () => {  ... }`
              },
              'app/components/test.hbs',
              { line: 0, character: 0 }
            )
          ).response

@patocallaghan
Copy link

Thanks @lifeart, appreciate the advice. All makes sense. I'll be picking this up again on Monday 🙇‍♂️

return null;
}

return typeof intlConfig === 'function' ? intlConfig() : intlConfig;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lifeart This is perplexing me a bit as to how I should treat this intlConfig. Essentially I want to return the IntlConfig back as an object here. Debugging jest is causing me a bit of pain to figure it out 😅

My debugger is telling me this is returned as function but even when I add intlConfig() it's still returned as a function 😬 Is there something obvious I'm missing here?

config: {
  'ember-intl.js': `module.exports = function() { return { wrapTranslationsWithNamespace: true } }`,
},

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, it may be jest problem, if it's acting as a function, try to toString() it to figure out what is it.
In general, if tests is passing, it should work fine.
Provided config is correct.

@patocallaghan
Copy link

@lifeart I think we're good to go now. It turned out to be some transient issue with my development environment 🤷‍♂️ Restarting VSCode (to finish an update) and clearing the Jest cache meant my debugging and testing started working as expected 🤦‍♂️

So I've noticed even though I can commit to Aaron's branch I don't have permissions to move this PR out of "Draft" and also to update the PR description. Could you do that for me and also add the following as the PR description please?


Fixes #344

Ember Intl exposes a configuration file which allows you to configure the localization setup. One such setting is wrapTranslationsWithNamespace which determines whether translation keys should have the folder names within the key names.

wrapTranslationsWithNamespace: false (default)

# translations/sub-folder/en-us.yml
subFolder:
  foo: bar
this.intl.t('subFolder.foo');

wrapTranslationsWithNamespace: true

# translations/sub-folder/en-us.yml
subFolder:
  foo: bar
this.intl.t('sub-folder.subFolder.foo');

wrapTranslationsWithNamespace: true is not currently supported by this extension. This PR adds the ability, when wrapTranslationsWithNamespace: true is set in app/config/ember-intl.js, to support keys which have folders in their key names.

@lifeart lifeart marked this pull request as ready for review July 22, 2022 10:18
@lifeart lifeart changed the title [EXPERIMENT] 🧪 Support folder namespaces feat: 🧪 Support folder namespaces for intl Jul 22, 2022
@lifeart lifeart merged commit 9099c60 into lifeart:component-context-info-origin Jul 22, 2022
lifeart pushed a commit that referenced this pull request Jul 22, 2022
# [2.26.0](v2.25.0...v2.26.0) (2022-07-22)

### Features

* 🧪 Support folder namespaces for intl ([#389](#389)) ([9099c60](9099c60))
@patocallaghan patocallaghan deleted the bugfix/support-folder-namespaces branch July 22, 2022 11:14
@patocallaghan
Copy link

Just tested in our app and this works now! Thanks for the help @lifeart 👏

@lifeart
Copy link
Owner

lifeart commented Jul 22, 2022

@patocallaghan @achambers Thank you for your work and contribution!
uELS 3.0.33 released :)

@lifeart
Copy link
Owner

lifeart commented Jul 22, 2022

@patocallaghan could you check this behaviour on your project? lifeart/els-intl-addon#6 (comment)

@patocallaghan
Copy link

@lifeart looking 👀

@patocallaghan
Copy link

Yep I've noticed that intellisense doesn't work in all cases. Looking into it now.

@toovy you mention the absolute path is off for your examples. How do you see the path?

Also any chance you could pull together a test case using the test syntax similar to https://github.com/lifeart/ember-language-server/pull/389/files#diff-65bd0ca4089daf08ddfa3876799a64c1dc87e2629dfae17a16030a0c0b5aa8caR14 just so I can replicate what you are seeing?

@toovy
Copy link

toovy commented Jul 22, 2022

@patocallaghan I try to be as helpful as I can, still, I'm pretty busy at the moment.

# Example from the translations/en-us.yaml
admin:
    countries: Countries

This is what it looks like when I use the intellisense when the string is still empty:

image

This is the result when e.g. choosing "Countries" (basically the complete path on my volume; correct would be {{t 'admin.countries'}}):

image

This is how our translations folder looks like:

image

No translation/no path outside the main YAML is taken into consideration for the intellisense at the moment.

BR Tobi

@patocallaghan
Copy link

Thanks @toovy 🙇‍♂️

@patocallaghan
Copy link

patocallaghan commented Jul 22, 2022

@toovy just curious but what editor are you using? I don't get any intellisense when it's {{t ""}} and also I can get my editor to show intellisense for translations in nested directories (although I've found some instances where it doesn't work but need to figure those out).

I might create a test app to verify because at the minute all the unit tests I write in this repo to find the bug pass 🤔

Edit: I'm assuming it's VS Code but thought I'd ask anyway as I get a different experience than you.

Edit: nvm found the bug 👍

@patocallaghan
Copy link

@toovy key name issue should be fixed by #392

lifeart pushed a commit that referenced this pull request Jul 22, 2022
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`
lifeart pushed a commit that referenced this pull request Jul 22, 2022
## [2.26.1](v2.26.0...v2.26.1) (2022-07-22)

### Bug Fixes

* key name when translation file is at the root ([#392](#392)) ([4294adb](4294adb)), closes [/github.com//pull/389#issuecomment-1192553091](https://github.com//github.com/lifeart/ember-language-server/pull/389/issues/issuecomment-1192553091)
return null;
}

return typeof intlConfig === 'function' ? intlConfig() : intlConfig;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patocallaghan I've been throwing this around in my head for a while. The config function takes an ember-cli environment argument. While we don't do this I wonder if there's a world where people set different config based on the environment. I wonder whether we should pass development in to the function just to be sure. Seeing as this is for intellisense, I think it's pretty safe to assume the ember-cli environment will be development at that point.

Thoughts?

(Did a code search and here's one such example. I know passing in development wouldn't affect the outcome here but it's an example of how the argument could be used.)

Copy link
Owner

@lifeart lifeart Jul 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I'm up with specifying env = 'development' to intl config function, it could hep us implement #335

@toovy
Copy link

toovy commented Jul 27, 2022

@toovy just curious but what editor are you using? I don't get any intellisense when it's {{t ""}} and also I can get my editor to show intellisense for translations in nested directories (although I've found some instances where it doesn't work but need to figure those out).

I might create a test app to verify because at the minute all the unit tests I write in this repo to find the bug pass 🤔

Edit: I'm assuming it's VS Code but thought I'd ask anyway as I get a different experience than you.

Edit: nvm found the bug 👍

Yes, VS Code, thanks for fixing this! What is the simplest way to try it out without waiting for the release?

Edit: Are there people using different IDEs at all? ;)

@patocallaghan
Copy link

@toovy This should be out already 👍 1728455

@lifeart
Copy link
Owner

lifeart commented Jul 27, 2022

yep, lifeart/vscode-ember@06a5d87

@toovy
Copy link

toovy commented Jul 27, 2022

@patocallaghan fix worked, no weird paths any more :) still, no nested files are analysed on my VSCode

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
4 participants