Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Namespaces / Nested translation keys #5

Open
alexbardas opened this issue Nov 8, 2015 · 9 comments
Open

Namespaces / Nested translation keys #5

alexbardas opened this issue Nov 8, 2015 · 9 comments

Comments

@alexbardas
Copy link

It sometimes makes sense to group different keys of a component under a namespace:

e.g.:

{
  "form": {
    "cancel": "Cancel",
    "save": "Save"
  }
}

And accessing them like this:

t('form.cancel')

The main selling points are expressiveness and reduced size for the translation json.

It's basically this angular-translate feature.

@bloodyowl
Copy link
Owner

this is possible, although this can be a breaking change in the current state of it (you can put . in your keys already), if I introduce this, it would likely be with translate()(Component) (with no argument). would this be solving your use-case?

@alexbardas
Copy link
Author

It can also be implemented as a non breaking change, but you may not want to do it like this if you consider it would be too confusing.

Consider you have the key form.cancel. If there is no match in the initial Component dictionary and the key has at least one ., then check if key.split('.')[0] is in the dict and its value is an object instead of a string.

@bloodyowl
Copy link
Owner

this approach would be too implicit and confusing IMO.

@Bertg
Copy link

Bertg commented Dec 16, 2015

We are working on some namespace system as well. However we don't think namespacing is a component issue; but a translator issue.

We are doing this for the translate method:

translate("Header", "Ui")(Header)

Where Ui is a fallback namespace for the keys to be looked into. We are also using a "global" fallback. So a translations like this: t('Menu') would be sought for in Header.Menu, Ui.Menu and global.Menu. A final fallback is to return the Key itself.

Maybe this can be implemented?

@lloydwatkin
Copy link

I'd be looking to do something like this, I'd refer to them as 'shared' translations, my example would be:

translate([ 'Header', 'MenuItems' ])(Header)
/* ..and... */
translate([ 'Footer', 'MenuItems' ])(Footer)

@Bertg
Copy link

Bertg commented Jan 18, 2017

Hey @lloydwatkin This is exactly what we have settled on in our internal translator.
We did not use this lib, and went for a different approach, but that is exactly what our method signatures now look like.

Internally when the translate function is called, it fetches each of the "shared" translations and merges those objects. This is then passed to the translator.

@lloydwatkin
Copy link

Would this work for you @bloodyowl, there wouldn't be a BC break since one is a string and the other an array?

@beau6183
Copy link

beau6183 commented Apr 26, 2017

#12

That PR accomplishes the original ask as:

t('form')['cancel']

However, it does not support pluralization or string expansion in that usage.

@lloydwatkin
Copy link

Getting to the point where I require this functionality so have implemented here #13, does not break existing implementations and supports all required features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants