Support namespace by directory for modules
See original GitHub issueIs 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:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Wow! Your solution seems more reasonable, I will implement it soon 😃 Thanks
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 asen/GB/**/*
instead ofen-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 likeapp/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).