question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support namespace by directory for modules

See original GitHub issue

Is your feature related to a specific framework or general for this extension General.

Is your feature request related to a problem? Please describe. Related to, but different from the request in #202 . My file structure for translations is as such:

i18n
  ├── general
  |   ├── nl.yaml
  |   ├── de.yaml
  |   ├── en.yaml
  |   └── ...
  ├── attributes
  |   ├── foo
  |   |   ├── nl.yaml
  |   |   ├── de.yaml
  |   |   ├── en.yaml
  |   |   └── ...
  |   ├── bar
  |   |    └── ...
  |   └── ...
  ├──  resources
  |    └── ...
  └── ...

These files contain keys like this:

# i18n/general/nl.yaml

title: Titel
user:
  first_name: Voornaam
  last_name: Achternaam

# i18n/attributes/foo/nl.yaml

model_name: quux
user:
  first_name: Roepnaam

These modules are dynamically requested and merged into the translation object, nested and namespaced under the directory structure of the translation module. Then, in my framework (vue-i18n) I request them like so:

# some-component.vue

{{ $t('general.title') }} (Titel)
{{ $t('general.user.first_name') }} (Voornaam)
{{ $t('attributes.foo.model_name') }} (quux)
{{ $t('attributes.foo.user.first_name') }} (Roepnaam)

I’m aware this may still cause namespace collisions if a directory has the same name as a nested key, but it would resolve a majority of collisions that I have right now where keys are treated as equal coming from different modules.

Describe the solution you’d like Similar to the fileNamespace: true option, having a pathNamespace: true option would work.

Alternatively, it could be refactored into a namespaceKeys: 'path' | 'file' | 'dir' | 'none' option. ‘dir’ would use both file and path namespacing.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
antfucommented, Feb 26, 2020

Wow! Your solution seems more reasonable, I will implement it soon 😃 Thanks

1reaction
Excalibaardcommented, Feb 26, 2020

Thanks for the quick response!

The difference between our examples comes down to ~/i18n/[lang](/[module])*/module.yml vs ~/i18n(/[module])*/lang.yml. Since in-/excluding the filename is already supported, it should be sufficient to check whether the path starts with a known locale to differentiate [lang] from [module]. I believe that’s a safe assumption (I don’t imagine people would willingly choose file structures such as en/GB/**/* instead of en-GB/**/*, or name their modules the same as their locale).

Alternative ways could be to determine a ‘namespaceRoot’ which is the static part of your path from where the module namespacing begins (probably easiest with regex: /app\/frontend\/src\/i18n\/\w+/ in addition to a way to turn path namespacing on/off. Inspired by webpack file-loader and vue-router params it could be immediately set in localesPath like app/frontend/src/i18n/[$nsRoot]/**/* to separate it manually, but I don’t like how that pollutes a perfectly fine basic setting and may not be good enough for some implementations.

A custom function is probably too flexible. Most people probably use a single translation file per locale, modularization (to this extent) is already quite niche. Even then, some implementations of this structure don’t use it to namespace (like rails-i18n, where every nested file just builds from the root, which is a pain to maintain ;p).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Namespaces and Modules - TypeScript
This post outlines the various ways to organize your code using modules and namespaces in TypeScript. We'll also go over some advanced topics...
Read more >
Packaging namespace packages
Namespace packages allow you to split the sub-packages and modules within a single package across multiple, separate distribution packages (referred to as ...
Read more >
Correct Namespace for Module Classes within modules folder
Hi All, Fairly new to ProcessWire so apologies if this is a daft question, but I am having a go creating my first...
Read more >
support namespace packing #167 - python-poetry ... - GitHub
toml file, first in the directory of the pyproject.toml file, then in the src/ subdirectory if it exists. If your root module cannot...
Read more >
Is there any option to namespace a module folder path in ...
The code should look like below and be usable in every folder level import { AuthService } from '@modules/auth/auth.service';.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found