Jump to content

Getting content in user language in templates outside of template folder


Juergen
 Share

Recommended Posts

Hello @ all,

I have a some templates which will be loaded via AJAX. They are located in a separate folder under site/ajax/ so they are not in the PW template folder.

I have bootstraped these templates via "include("../../index.php");" so I can query PW fields via API calls. It works quite well and I get all values from my PW fields.

Unfortunately if I switch the language from German to English, all values will stay in the default language (German). So the language change will not be taken into account, because there are no different urls for each language (no PW pages).

Has someone dealed with the same problem and could give me a hint how to take the language change into account?

My idea was to add a query string with the id of the language at the end of the AJAX href (like "path/to/myajaxtemplatefile?id=1008) but maybe someone has a better idea. I dont want to create a real PW page for such small piece of information.

 

Best regards

  • Like 1
Link to comment
Share on other sites

24 minutes ago, Juergen said:

My idea was to add a query string with the id of the language at the end of the AJAX href (like "path/to/myajaxtemplatefile?id=1008) but maybe someone has a better idea.

That's basically how I have handles it in the past:

path/to/myajaxtemplatefile.php?lang=portugues

Then in the myajaxtemplatefile.php file add this to set the language:

$user->language = $languages->get($sanitizer->selectorValue($input->get->lang));

 

  • Like 2
Link to comment
Share on other sites

1 hour ago, Juergen said:

I dont want to create a real PW page for such small piece of information

Why not? It's not that much work and provides you with all the niceties of language support. Drop a simple PHP template in the regular templates folder:

<?php
include("../site/ajax/{$input->urlSegment1}.php");

and create a template with url segments enabled with pattern:

regex:^[a-zA-Z0-9]+\.php$

A single, hidden page can then route all your ajax calls in a language-aware way. Altogether that shouldn't take more than five minutes.

  • Like 6
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...