Select specific language api route in query field

Hello,

I have two routes which generates a json structure.
One route is for french the other one for english.
The json datas are coming from input written into the panel by the user.


      [
        'pattern' => 'fr/myapi/subprog-items.json',
        'language' => 'fr',
      	'action' => function () {
          header('Content-type: application/json; charset=utf-8');
      		$json = array();
      		foreach (site()->residency()->toStructure() as $residencies) {
      			foreach ($residencies->subprog()->toStructure() as $subprog) {
              $json[] = array(
                  'title' => (string)$subprog->subprogtitle(),
              );
      			}
      		}
      		return Json::encode($json);
      	}
      ],

     [
        'pattern' => 'en/myapi/subprog-items.json',
        'language' => 'en',
      	'action' => function () {
          header('Content-type: application/json; charset=utf-8');
      		$json = array();
      		foreach (site()->residency()->toStructure() as $residencies) {
      			foreach ($residencies->subprog()->toStructure() as $subprog) {
              $json[] = array(
                  'title' => (string)$subprog->subprogtitle(),
              );
      			}
      		}
      		return Json::encode($json);
      	}
      ],

I would like to get the adress of each routes depending on the selected language into a query field.
With something like that:


subprog:
  label: subprog
  type: select
  width: 1/4
  options: api

  api:
    url:
      fr: http://localhost:8888/00-AR/fr/myapi/subprog-items.json
      en: http://localhost:8888/00-AR/en/myapi/subprog-items.json
    text: "{{ item.title }}"
    value: "{{ item.title }}"

Is this possible or should I try to manage the language display via the routes?

Thanks a lot for any suggestions.

Since you cannot use two or more different routes, you have to deal with the languages in a single route.

1 Like

Thanks for your reply, here is how it turned out:


'routes' => [
      [
        'pattern' => '(:any)/myapi/projects-items.json',
        'language' => '*',
      	'action' => function ($language) {
          header('Content-type: application/json; charset=utf-8');
      		$json = array();
      		foreach (site()->projects()->toStructure() as $projects) {
      			foreach ($projects->project-item()->toStructure() as $project) {
              $json[] = array(
                  'title' => (string)$project->project_title(),
              );

      			}
      		}
      		return Json::encode($json);
      	}
      ],

    ]

and the query in the blueprint :

          project-item:
            label: project-item
            type: select
            width: 1/4
            options: api

            api:
              url: {{ site.url }}/myapi/projects-items.json
              text: "{{ item.title }}"
              value: "{{ item.title }}"

Sounds quite silly now but I was kind of struggling on how to deal with it in the blueprint.