3

Using the locale modul (core) and i18n it is possible to show a language switcher, which will guide the user to corresponding nodes in another language. In case of views, this language switcher points to a view in every language. The views path i.e.

www.website.com/path/to/view

gets "translated" to every language

www.website.com/language-path/path/to/view.

Is it possible to change this behaviour or to restrict the view to specific languages?

  • Something like redirect using: drupal.org/project/redirect ?! – Mohammad Ali Akbari Feb 17 '13 at 19:54
  • I dont want to redirect the user after visiting the view in another language, but alter the links shown in the language switcher. – jumper Feb 18 '13 at 21:31
  • how do you tell the view what language is should be ? – rémy Feb 18 '13 at 22:35
  • 1
    What do you mean by "change this behaviour" ? If you want to map different urls corresponding to the same view in different languages, this answer will help you : drupal.stackexchange.com/questions/53175/… – claire_ Feb 23 '13 at 15:23
  • 2
    ... Or you could just filter your view for the user's current language + set "No results behavior" to display a particular node (404 or other) – claire_ Feb 23 '13 at 15:35
3
+50

If you want to map different URLs corresponding to the same view in different languages, this answer will help you. Basically you have to enable "Translation sets" and "Path translation" modules, both part of i18n module. Then in admin/config/regional/i18n_translation/path, add the translated paths and then add a "translation set" which will map all these pages together.

If you want to restrict the view to specific languages because it yields no results for the other languages, you can filter your view results by the user's current language and set a "No results behaviour" to display the view results for a different (default) language, using a Global: View area. See this question for more details. Of course you can also display a specific node (404 page or other) as a "No results behaviour".

@jumper: I've put this answer in case you want to give the bounty :)

  • Thanks a lot. I edited your answer a little bit to include the actual solution right here. – jumper Feb 24 '13 at 20:30
2

yes you can restrict the views language by adding filter criteria for content:language if you want to restrict it based on the content language or by user:language if you want to restrict/filter it based on the preferred language for the current logged user

enter image description here

  • Thank you very much. I dont want to change the view depending on the users language, but hide the view in specific languages. – jumper Feb 24 '13 at 11:33
  • then you should add the filter criteria Content:Language which will filter the view content for a specific language, which mean if you don't have any content for that language it will return an empty view, but if you want to hide the entire view page for a specific language, you can install "Rules" Module and create a rule that will redirect the user to another page (which you you chose) basing your conditions on for that language and the path to the view. – Mouad Feb 24 '13 at 22:39
0

You can use Select Translation.

If your view is meant to show nodes, and your nodes use the Content Translation* mechanism, you can install the Select Translation module. It provides a filter "Content translation: Select translation" by which you can define a fallback mechanism to select which language version of results should be used.

For example, you can specify that if a result is not available in the current language, its English version is used. So if no results are available for the current language, as in your case, this is equivalent to visiting the English version of the view. So effectively this "restricts the view to specific languages" resp. "hides the view in specific languages" as you asked for.

*Content Translation: That's the default translation mechanism in Drupal 7, available in Core. So if you did not install the alternative (Entity Translation), you are using this one.

0

You can rewrite the language filter in custom code.

I know you only want to modify the view if it has no content to show in the selected language. Here's a solution that can do that, but also allows you to modify the view results even if there are results in the currently selected language:

  1. Add a language filter to your view:

    • If you use Entity Translation, add the "Content: Language" filter.
    • If you use Content Translation, add the "Content Translation: Language" filter.
    • If you use Content Translation and want automatic language fallback behaviour for each result, you can use the "Content translation: Select translation" filter instead, provided by the Select Translation module.
  2. Implement hook_views_query_alter() and change the parameters of your language filter as needed. For details, see this answer. For example, you can implement logic such as "If the current language is Chinese, set the language filter to show English content".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.