What are the best methods for JSON response from a multilingual API

I wanted to add languages ​​to the current API response in English. The current API response is as follows:

{ status : "success", data : { query: "contains search query", queryType: "search" results: [ { title: "Marvel Captain America T-shirt", price: 624, category: "t-shirt", gender: "men", }, ..... ], language: "english", currency: "rupee", } } 

There are two ways to develop an API, one could use different kernels to respond to the API and make calls according to the language preferences in the application, but my application also requires saving some English elements. Another way is to put all languages ​​in the answer with a specific structure in order to distinguish each. In any case, I want to keep the English elements.

So, if gender: "men", but the language is French, so I want an additional field, for example gender_display: "hommes" or gender_fr: "hommes"

Addition: search results come from Solr

+6
source share
2 answers

When it comes to: [ Internationalization | globalization | localization ] one of the most popular standards is i18n . From the point of view of coding, we are talking about creating a "resource dictionary".

Different libraries have implemented this standard, and their principles span from translations to conversions when it comes to things like currencies, dates and numbers.

For example, i18n-js offers this structure:

 I18n.translations = { en: { hello: "Hello World!" }, "pt-BR": { hello: "Olá Mundo!" }, "de": { hello: "Hallo Welt!" }, "nb": { hello: "Hei Verden!" } }; I18n.t("hello", {locale: "en"}); //returns "Hello World!" I18n.t("hello", {locale: "pt-BR"}); //returns "Olá Mundo!" 

There is a jQuery plugin that can also help:

 $.i18n.load({ a_key: 'translated string %2$s - %1$s' }); $.i18n._('a_key', ['order', 'in'])); //returns 'translated string in - order' 

Conclusion

To determine the structure of your JSON, it is recommended that you first select a Framework that is best suited to your needs and follow the recommendations of the community. This will make decision-making more oriented and reinforced by community experience.

+2
source

One question you can ask yourself:

How many languages ​​and different editors will work on the API files for each language, each of which specializes in one language? If you have another editor in one language, you may need one [set] of API files for each language. However, instead of repeating everything for each language (for example, the price remains the same for everyone, for example), you can have a master API installed in one language and just store only what differs from the master in other localized sets of APIs.

Suppose you code (as an example implementation) JavaScript and jQuery, then you can use the jQuery extension function to easily overlay the localized API on top of the main API. The advantage of this is that if there is no translation in your localization file, you will return to the wording of the main API.

Warning. This may not be the best approach for your situation, since you provided only general information. Hopefully this suggestion is helpful or useful for further thought. I also made the assumption that you are dealing with API files, as this often happens with localization, but you may have database content or something else.

+1
source

All Articles