Answers.Body: <p>To answer my own question see below:</p>
  
  <p>As <a href="https://craftcms.stackexchange.com/users/125/carlcs">carlcs</a> pointed out correctly the problem actually isn't located in getting the category - which works just well and is the right way to do it - but in how the relation to the entries is done.</p>
  
  <ol>
  <li>I should check if the tag (coming from the url) actually resolves to a "real" category, otherwise just all entries will be shown.</li>
  <li>just handling over the category to the <code>.relatedTo</code> method on craft.entries won't do it because that just fetches all entries related to this category irrespective to its locale - <code>sourceLocale: craft.locale</code> needs to be set.</li>
  <li>Still not finished; because we want to "<em>find elements related to the given element, where the given element is the target of the relation</em>" – target in this sense is the category – we need to define it as <code>tagetElement</code> in the <code>relatedTo</code> method.</li>
  </ol>
  
  <p>...</p>
  
  <p>Getting the entries should now look like this:</p>
  
  <pre><code>{% if categories|length %}
      {% set entries = 
                craft.entries.section('projects').relatedTo({
                   targetElement: categories,
                   sourceLocale: craft.locale
                }) 
      %}
      {% for entry in entries %}
      ...
      {% endfor %}
  {% endif %}
  </code></pre>
  
  <p>...</p>
  
  <p>And the actual title of my question should be <strong>"How to do category relations with locales"</strong> ;)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T13:32:38.963Z
Answers.Text: Nice catch with that `sourceLocale` parameter! Setting `targetElement` is probably not necessary, `element` should do it if there's no other relations between these elements.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T13:13:28.713Z
Answers.LastActivityDate: 2015-04-01T13:13:28.713Z
Answers.LastEditDate: 2017-04-13T12:51:00.160Z
Answers.UserDisplayName: m9dfukc
Answers.Body: <p>An elements <code>slug</code> property is saved per locale, so you shouldn't get categories with a slug "culture" in your german locale. The <code>locale</code> property is set to your current locale by default, so there's no need to set that explicitly.</p>
  
  <p>I just did a simple test to see if there's no exception to category ElementCriteriaModels.</p>
  
  <pre><code>{% set categories = craft.categories.slug('alkoholfrei') %}
  {{ categories|length ? categories.first().title }}
  </code></pre>
  
  <p>And I can confirm it works as expected, a category with a translated german slug of "alkoholfrei" is not returned in other locales.</p>
  
  <p><s>I would recommend to double check your category slugs, you probably only translated the title and your slug is not yet updated.</s></p>
  
  <p>But the actual problem with your code is not how you get the categories, but how you're dealing with them in the <code>craft.entries</code> ElementCriteriaModel. You should check if there's actually categories found, because otherwise your <code>relatedTo</code> param doesn't do anything and you simple get all your "project" entries returned.</p>
  
  <pre><code>{% if categories|length %}
      {% set entries = craft.entries.section('projects').relatedTo(categories) %}
      {% for entry in entries %}
          ...
      {% endfor %}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T12:14:16.690Z
Answers.Text: I just double checked - the slug's are translated for sure. Outputting the categories slug (and title); on the english page I get `culture` and on the german page I get `kultur`. Getting the right category doesn't seem the problem ... It looks more like the issues is with the `relatedTo` method not distinguishing between the locales of the category?!
Answers.UserDisplayName: m9dfukc
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T12:36:48.370Z
Answers.Text: Ahhh, now I think I know where the mistake is, will edit the answer...
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T12:47:44.777Z
Answers.Text: I've oversimplified the code to be more clear ... actually I do check if there where categories found. Anyway ... I think I found the answer, which has to do setting targetElement and locale explicitly in relatedTo. ... I'll submit a separate answer.
Answers.UserDisplayName: m9dfukc
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T11:41:12.547Z
Answers.LastActivityDate: 2015-04-01T12:42:17.227Z
Answers.LastEditDate: 2015-04-01T12:42:17.227Z
Answers.UserDisplayName: carlcs
Body: <p>I've setup a localized site (de/en) with custom routing for my categories (which are marked as translatable). I use a <em>tag route</em> for my categories which points to the same template as my structure. Everything seemed to work fine but then I realized that the custom filter (via relatedTo) also outputs entries that don't belong to the current category locale.</p>
  
  <p>To make it clear I'll try to give a rough schemata of my setup (using Craft Pro 2.3.2640):</p>
  
  <p><strong>section</strong> <em>projects</em>:</p>
  
  <pre><code>has the entry url format:
    de -&gt; projekte/{slug}    
    en -&gt; projects/{slug}
  entry template path:
    projects/_entry
  </code></pre>
  
  <p><strong>single</strong> <em>projectsIndex</em>:</p>
  
  <pre><code>has the entry url format:
    de -&gt; projekte  
    en -&gt; projects
  entry template path:
    projects/index
  </code></pre>
  
  <p><strong>routes</strong>:</p>
  
  <pre><code>de -&gt; projekte/(tag)
    template path: projects/index
  en -&gt; projects/(tag)
    template path: projects/index
  </code></pre>
  
  <p><strong>category</strong>:</p>
  
  <pre><code>doesn't have a it's own url
  category field is marked as translatable
  </code></pre>
  
  <p><strong>category entries</strong> (german version and the english translation):</p>
  
  <pre><code> - de -&gt; kultur      en -&gt; culture    | (resp. title and slug)
   - de -&gt; industrie   en -&gt; industry   | (resp. title and slug)
   ...
  </code></pre>
  
  <p><strong>project entries</strong>:</p>
  
  <pre><code> - Entry A:
       de -&gt; category relations ['industrie', 'kultur']
       en -&gt; category relations ['industry']
   - Entry B:
       de -&gt; category relations ['kultur']
       en -&gt; category relations ['culture']
  </code></pre>
  
  <p><strong>template projects/index.html</strong>:</p>
  
  <pre><code>...
  {% set _filter = tag|default(null) %}
  {% set _category = craft.categories.slug(_filter) %}
  ...
  {% for entry in craft.entries.section('projects').relatedTo(_category) %}
     ...
  </code></pre>
  
  <p><strong>urls</strong> (here comes the trouble):</p>
  
  <pre><code> - /projekte
       -&gt; lists german version of 'Entry A' and 'Entry B'  (expected)
   - /en/projects
       -&gt; lists english version of 'Entry A' and 'Entry B' (expected)
  
   - /projekte/kultur
       -&gt; lists german version of 'Entry A' and 'Entry B'  (expected)
   - /en/projects/culture
       -&gt; lists english version of 'Entry A' (NOT expected) and 'Entry B'
  </code></pre>
  
  <p>...'Entry A' on '/en/projects/culture' shouldn't show up just because there's the 'kultur' category attached to the german version of this entry!</p>
  
  <p>I already tried to explicitly restrict the '_category' by using the '.locale' method but that didn't help either. Now I wonder if I'm doing something fundamentally wrong or if this is a bug by craft itself. </p>
  
  <p>Any tips are appreciated. </p>
  
  <p>Thanks</p>
  
  <p><strong>Update:</strong> 
  Answering my own question <a href="https://craftcms.stackexchange.com/a/8957/3323">here</a>.</p>
  
Tags: <localization><relations><category>
Title: How to use localized categories properly?
ThreadText: # How to use localized categories properly?
  
  (Asked by: username_0 on 2015-04-01)
  
  I've setup a localized site (de/en) with custom routing for my categories (which are marked as translatable). I use a *tag route* for my categories which points to the same template as my structure. Everything seemed to work fine but then I realized that the custom filter (via relatedTo) also outputs entries that don't belong to the current category locale.
  
  To make it clear I'll try to give a rough schemata of my setup (using Craft Pro 2.3.2640):
  
  **section** *projects*:
  
  ```
  has the entry url format:
    de -> projekte/{slug}    
    en -> projects/{slug}
  entry template path:
    projects/_entry
  
  ```
  
  **single** *projectsIndex*:
  
  ```
  has the entry url format:
    de -> projekte  
    en -> projects
  entry template path:
    projects/index
  
  ```
  
  **routes**:
  
  ```
  de -> projekte/(tag)
    template path: projects/index
  en -> projects/(tag)
    template path: projects/index
  
  ```
  
  **category**:
  
  ```
  doesn't have a it's own url
  category field is marked as translatable
  
  ```
  
  **category entries** (german version and the english translation):
  
  ```
   - de -> kultur      en -> culture    | (resp. title and slug)
   - de -> industrie   en -> industry   | (resp. title and slug)
   ...
  
  ```
  
  **project entries**:
  
  ```
   - Entry A:
       de -> category relations ['industrie', 'kultur']
       en -> category relations ['industry']
   - Entry B:
       de -> category relations ['kultur']
       en -> category relations ['culture']
  
  ```
  
  **template projects/index.html**:
  
  ```
  ...
  {% set _filter = tag|default(null) %}
  {% set _category = craft.categories.slug(_filter) %}
  ...
  {% for entry in craft.entries.section('projects').relatedTo(_category) %}
     ...
  
  ```
  
  **urls** (here comes the trouble):
  
  ```
   - /projekte
       -> lists german version of 'Entry A' and 'Entry B'  (expected)
   - /en/projects
       -> lists english version of 'Entry A' and 'Entry B' (expected)
  
   - /projekte/kultur
       -> lists german version of 'Entry A' and 'Entry B'  (expected)
   - /en/projects/culture
       -> lists english version of 'Entry A' (NOT expected) and 'Entry B'
  
  ```
  
  ...'Entry A' on '/en/projects/culture' shouldn't show up just because there's the 'kultur' category attached to the german version of this entry!
  
  I already tried to explicitly restrict the '\_category' by using the '.locale' method but that didn't help either. Now I wonder if I'm doing something fundamentally wrong or if this is a bug by craft itself.
  
  Any tips are appreciated.
  
  Thanks
  
  **Update:**
  Answering my own question [here](https://craftcms.stackexchange.com/a/8957/3323).
  
  **Tags:** localization, relations, category
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2015-04-01. Score: 5 | Accepted answer
  
  To answer my own question see below:
  
  As [username_1](https://craftcms.stackexchange.com/users/125/username_1) pointed out correctly the problem actually isn't located in getting the category - which works just well and is the right way to do it - but in how the relation to the entries is done.
  
  1. I should check if the tag (coming from the url) actually resolves to a "real" category, otherwise just all entries will be shown.
  2. just handling over the category to the `.relatedTo` method on craft.entries won't do it because that just fetches all entries related to this category irrespective to its locale - `sourceLocale: craft.locale` needs to be set.
  3. Still not finished; because we want to "*find elements related to the given element, where the given element is the target of the relation*" – target in this sense is the category – we need to define it as `tagetElement` in the `relatedTo` method.
  
  ...
  
  Getting the entries should now look like this:
  
  ```
  {% if categories|length %}
      {% set entries = 
                craft.entries.section('projects').relatedTo({
                   targetElement: categories,
                   sourceLocale: craft.locale
                }) 
      %}
      {% for entry in entries %}
      ...
      {% endfor %}
  {% endif %}
  
  ```
  
  ...
  
  And the actual title of my question should be **"How to do category relations with locales"** ;)
  
  > **Comments:**
  > **username_1**: Nice catch with that `sourceLocale` parameter! Setting `targetElement` is probably not necessary, `element` should do it if there's no other relations between these elements.  
  
  ---
  
  ### Answer by username_1 on 2015-04-01. Score: 2
  
  An elements `slug` property is saved per locale, so you shouldn't get categories with a slug "culture" in your german locale. The `locale` property is set to your current locale by default, so there's no need to set that explicitly.
  
  I just did a simple test to see if there's no exception to category ElementCriteriaModels.
  
  ```
  {% set categories = craft.categories.slug('alkoholfrei') %}
  {{ categories|length ? categories.first().title }}
  
  ```
  
  And I can confirm it works as expected, a category with a translated german slug of "alkoholfrei" is not returned in other locales.
  
  ~~I would recommend to double check your category slugs, you probably only translated the title and your slug is not yet updated.~~
  
  But the actual problem with your code is not how you get the categories, but how you're dealing with them in the `craft.entries` ElementCriteriaModel. You should check if there's actually categories found, because otherwise your `relatedTo` param doesn't do anything and you simple get all your "project" entries returned.
  
  ```
  {% if categories|length %}
      {% set entries = craft.entries.section('projects').relatedTo(categories) %}
      {% for entry in entries %}
          ...
      {% endfor %}
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: I just double checked - the slug's are translated for sure. Outputting the categories slug (and title); on the english page I get `culture` and on the german page I get `kultur`. Getting the right category doesn't seem the problem ... It looks more like the issues is with the `relatedTo` method not distinguishing between the locales of the category?!  
  > **username_1**: Ahhh, now I think I know where the mistake is, will edit the answer...  
  > **username_0**: I've oversimplified the code to be more clear ... actually I do check if there where categories found. Anyway ... I think I found the answer, which has to do setting targetElement and locale explicitly in relatedTo. ... I'll submit a separate answer.  
  
  ---
  
  