Jump to content

Recommended Posts

Posted

Hi forum,

I'm implementing a site where a files field needs to be language-sensitive. I don't mean descriptions, but the files themselves need to be different according to language.

I could have a field for each language, but that would mean having to add new fields if we add new languages later on.

So my attempt was to use the description as a filter. If there is one, show the file, if not ignore it. So a file that has a description only for english would only show when viewing english. But the description defaults to the main language.

Is there a way to get the actual description on a given language without defaulting when it's empty?

Or some other approach that's minimally elegant for this problem.

 

Thanks,
H

Posted
Just now, heldercervantes said:

Thx adrian, but this doesn't seem to work for file fields. Even with outputformatting set to false I always get the default language.

Works great for file fields for me - I use it for different language versions of the same PDF report.

I don't think you want outputformatting off though - you'd want it on to get the localized version of the file and its description.

Maybe someone with more ML experience might have an idea why it's not working for you?

 

Posted
Just now, heldercervantes said:

I think I misinterpreted you.

Your approach is to add a file field for each language?

Correct - that is how those language alternate fields work. Just add the name of the language as a suffix to the base field name and it should all be taken care of automatically. Maybe not the cleanest solution because you do need a new field for each language, but other than that it works seamlessly in my experience.

Posted

Found a pretty cool solution for this. Repeater + Page + Files.

So I set up a repeater called langFiles. Inside that repeater I added a Page selector called lang and the files field.

The page selector is set to be a single select, by template = language.

So in the admin I get a repeater, add an item, select the language and upload the files. Add another one for the second language, and so on. 

Then on the frontend:

// Somehow $page->langFiles->find('lang='.$user->language) doesn't work
foreach ($page->langFiles as $lf) {
  if ($lf->lang == $user->language) {
    $filesRepeaterBlock = $lf;
    break;
  }
}

if ($filesRepeaterBlock->files->count) {
  echo '<ul>';
  foreach ($filesRepeaterBlock->files as $file) {
    echo '<li><a href="'.$file->url.'" target="_blank">'.$file->name.'</a></li>';
  }
  echo '</ul>';
}

And there it is. Add a new language tomorrow, and it adapts. And it's not too weird for the admin.

  • Like 2
Posted

Nice one!

25 minutes ago, heldercervantes said:

// Somehow $page->langFiles->find('lang='.$user->language) doesn't work

Try this:

$page->langFiles->find('lang.name='.$user->language) 

 

Posted

Already did, no cigar.

If I get the first langFiles->lang, it returns 1010, same as outputting $user->language. Comparing those in an IF statement works, but the find returns nothing.

I tried:

find('lang.name='.$user->language->name);
find('lang='.$user->language->id);
find('lang.id='.$user->language->id);

Nothing works.

I even tried adding a title field and fake a string and still get nothing. Something's preventing that from working. But that foreach solves the problem and in this small site performance won't be an issue so I give up streamlining more.

 

 

 

Posted

Yes of course - sorry, you can't actually "find" a specific repeater item.

However one thing you might prefer is rather than using a Page field for the language, just use the built in "language" field.

Posted
8 hours ago, adrian said:

Yes of course - sorry, you can't actually "find" a specific repeater item.

Maybe this issue is specific to language pages when used in a Page field, because I can successfully find repeater items with a Page field matching a given id, name, title, etc.

Posted
2 minutes ago, Robin S said:

Maybe this issue is specific to language pages when used in a Page field, because I can successfully find repeater items with a Page field matching a given id, name, title, etc.

Can you find the actual repeater item, or just the page that has a repeater item that matches the find?

Posted
15 minutes ago, adrian said:

Can you find the actual repeater item, or just the page that has a repeater item that matches the find?

The actual repeater item. I also installed Language Support and set up a test Page field with template=language and that works too.

$results = $page->test_repeater->find("test_page.name=german");
echo $results->each("<p>{name}</p>"); // echoes the repeater item name, e.g. 1484443861-212-1

Edit: fixed the above

  • Like 1
Posted

Just trying this here:

d($page->repeatertest->find("language=".$user->language)->each("author_name"));

In this case I am using the system "language" field, rather than a page field and I am reporting the author_name which is another field within the repeater.

It actually seems ok here :)

  • Like 1

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
  • Recently Browsing   0 members

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