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

Add a translation api controller to the Delivery API #15132

Conversation

ToxicKevinFerm
Copy link

@ToxicKevinFerm ToxicKevinFerm commented Nov 4, 2023

Internationalization is important, so I would like a way to fetch Umbraco dictionary items. Preferably it would be using the request language, and return a json format similar to https://www.i18next.com/misc/json-format as a good standard in the field.

Prerequisites

Add a few translation items and test the API in /umbraco/swagger/

Description

I added a TranslationApiControllerBase that creates a new route and section in the API Explorer "translation".
Then I added a new controller endpoint GetListTranslationApiController with an optional startItem param that fetches dictionary values from LocalizationService and writes them to a json format.

I'm not sure you need to be able to fetch a single item, but you can with startItem.
The idea is to fetch your translations for a given language and cache it on the client side.

Here's where I'm a bit unsure of the Umbraco internals, do we need to index the dictionary items from LocalizationService? Do we need a cache or is the service fetching cached items already?

This is a work in progress, and I'm not sure what your thoughts are on adding new controllers to the Delivery API, but I feel like it'd be a good addition. It's also my first time looking into the Umbraco Core, so I might be totally wrong on best practices and such.

I'm not sure what you want the Delivery API to be going forwards, but I would consider dictionary to be part of the content.

that fetches the translations for the current request culture in a JSON format that can be used with https://www.i18next.com/
Copy link

github-actions bot commented Nov 4, 2023

Hi there @ToxicKevinFerm, thank you for this contribution! 👍

While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • 💡 The contribution looks original and the contributor is presumably allowed to share it

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

@busrasengul
Copy link
Contributor

Thank you @ToxicKevinFerm
I'm not 100% sure about extending the Delivery API in the core so I'll ask HQ to look into this.
But it looks like a great way to do when needed!

@ToxicKevinFerm
Copy link
Author

Thank you @ToxicKevinFerm I'm not 100% sure about extending the Delivery API in the core so I'll ask HQ to look into this. But it looks like a great way to do when needed!

Hi! Yes, that's what I was thinking as well, it does look pretty clean right now.

But I do think there is functionality missing that would fit very well there considering you might want to build a website with Next.js for example. The Delivery API does that very well today, but leaves something to be desired when you want dictionary values.

It could just as well be a plugin extending the delivery api, but I thought I'd give it a shot here first =)

@kjac
Copy link
Contributor

kjac commented Nov 6, 2023

Hi @ToxicKevinFerm,

Thanks a lot for this ❤️

We're going to have a little pow-wow at HQ to figure out where the Dictionary places itself in respect to the Delivery API. Unfortunately we can't gather all relevant parties until early next week, so please have a little patience with us 😄

I'll get back to you here when I have something to share.

@kjac kjac self-assigned this Nov 6, 2023
@kjac kjac self-requested a review November 6, 2023 12:08
@ToxicKevinFerm
Copy link
Author

Hi @ToxicKevinFerm,

Thanks a lot for this ❤️

We're going to have a little pow-wow at HQ to figure out where the Dictionary places itself in respect to the Delivery API. Unfortunately we can't gather all relevant parties until early next week, so please have a little patience with us 😄

I'll get back to you here when I have something to share.

Great! Thank you =) I'm open to changes for sure

@kjac
Copy link
Contributor

kjac commented Nov 15, 2023

Hi @ToxicKevinFerm.

Thank you again for contributing to Umbraco 💪

We agree that a Translation Delivery API would be a nice addition to the core. However, at this point there are a few things that prevent it. The main thing is that the LocalizationService queries the database directly - the dictionary is not cached 😢

To build a Translation API we first need to figure out a caching strategy for dictionary items.

The next thing to consider is how to shape the API. Translations have historically been utilized in a number of different ways for any number of different purposes. When we add a core API, we need to make sure we've covered all (or most of) those use cases. So... we need to talk to people 😄

To start this whole process I have opened a discussion here: #15214 - feel free to chime in 👍

I'm going to close this PR now in favour of the discussion. I have cross-linked the discussion and the PR so we retain the history. At least for the time being, people will have to create their own Translation APIs to cater for their specific use cases.

⚠️ For future reference, if anyone stumbles upon this: Be VERY mindful of how you expose dictionary items. The uncached nature of LocalizationService means you'll introduce a potential attack vector for malicious users if you're not careful.

@kjac kjac closed this Nov 15, 2023
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

Successfully merging this pull request may close these issues.

None yet

3 participants