15
votes
Accepted
Craft 3 language switcher
The concept of localization has changed in Craft 3, there are not only locales anymore - now you have different sites and site groups.
Example without multisite - only different locals
You can get ...
- 19.6k
10
votes
How can I build a language switcher?
Thought I would share my language switcher, as it shows languages in their native name.
<nav class="nav nav--locale">
<ul>
{% set locales = craft.i18n.getSiteLocales() %}
...
- 301
10
votes
Accepted
Get locale entry.title in other languages
You can use the 'locale' and 'id' attributes in your query to request the same entry in another locale.
{{ entry.title }}<br>
{{ craft.entries.id([entry.id]).locale('nl_nl').first.title }}
...
- 13.4k
10
votes
Accepted
Can I choose a language for the translate filter?
Yes. You can see all of the arguments the |t filter supports by looking at the docs for Craft::t(), the function that |t maps to. Its $language argument defines the target language translation to use. ...
- 34k
9
votes
How can I build a language switcher?
I've created a plugin which makes it incredibly easy to add language-switching links to your website...
Language Link plugin for Craft CMS
Standard Usage
Add this to your _layout, or create a ...
- 23.8k
8
votes
Accepted
How to add locales in Craft 3
"Sites" in Craft 3 replaces the old "Locales" concept in Craft 2.
There's not much documentation on them yet, but the setup process is virtually the same as it is in Craft 2 for locales. The main ...
- 67k
8
votes
Accepted
Craft 3: how can I set dynamic Base URL in sites setting?
So as of Craft CMS 3 RC7, they added a new alias() Twig function.
So what you can do is something like this in your general.php:
'aliases' => [
'@basePath' => getenv('CRAFTENV_BASE_PATH'),
...
- 11.3k
7
votes
Accepted
Can locales be used to switch layouts?
Untested, but should be fairly straightforward.
{% if craft.locale == 'de' %}
{% set template = '_layout_de.html' %}
{% else %}
{% set template = '_layout.html' %}
{% endif %}
{% extends ...
- 13.4k
7
votes
Accepted
Is there a way to have checkbox field labels translatable?
Short of hardcoding all these checkbox values and applying the translate tag on them
That's actually the only way to translate field names, checkbox labels/values and whatnot: taking advantage of ...
- 21.7k
6
votes
Accepted
Problems with channel and locales
Okay, so I found the answer. And the reason it wasn't working is because I've been using another .htaccess (a hybrid of h5bp and Craft) than the default that comes with the Craft download. And this ....
- 431
6
votes
Accepted
Export Craft Data
One way to handle this is by using the Export and Import tools by Bob Olde Hampsink. This will allow you to output the data to a CSV file, which you can hand off to the translator. Be wary though, if ...
- 23.8k
6
votes
Accepted
Multi Country website - Using locales is the best approach?
Yes you can do this by going to the craft/app/framework/i18n/data folder and copying the language file for each occurrence. You must give each a unique name es_pn, es_co, es_nc. Inside each of those ...
- 2,797
6
votes
Accepted
Adding new locale - hangs at Step 3 'ResaveAllElements'
You probably need to up Craft's internal memory limit, as well – the following should go into the array in your general.php configuration file:
'phpMaxMemoryLimit' => '1024M',
- 21.7k
6
votes
Accepted
Override admin local name
You can add your own translations by placing a region-specific translation file in a craft/translations folder. The filename should be named after your locale and return an array of translations. In ...
- 13.3k
6
votes
Accepted
Scaling channels with 35 locales
You could have a single News section that is enabled for each of the locales, and choose which locale each entry should target from their Edit pages.
To make it easer on the authors, you can set the ...
- 34k
6
votes
Accepted
Craft 3 Multi-site with no "default" site
This can be done in 3 main steps.
1. Get the site subdirectories working
First just get the /en/ and /es/ subdirectory working:
Create en/ and es/ subfolders in your webroot
Each subfolder should ...
- 34k
6
votes
Accepted
What is the difference between the Translation Methods when creating a new site
Not translatable
The field will always have the same value for each site
Translate for each site
Easy: the field can have a different value for each of your sites. Create 10 sites, all can have ...
- 19.6k
5
votes
Accepted
How to translate de postdate format for a multilanguage site
Instead of:
{{ entry.postDate.format('d M Y') }}
Use:
{{ entry.postDate | date('d M Y') }}
That should work.
- 6,318
5
votes
How to change the locals name in Localized Sites
You could add new pseudo localization files to Craft craft/app/framework/i18n/data/ and rename (translate) the name of your "language" and other things in these files.
I'd use en.php as a starting ...
- 36k
5
votes
How can I build a language switcher?
This simple solution has been working brilliantly for me...
{% set pageUri = craft.request.url|replace(siteUrl, '') %}
<ul>
<li><a href="{{ craft.config.siteUrl['en'] ~ pageUri }}"...
- 23.8k
5
votes
Accepted
How to use localized categories properly?
To answer my own question see below:
As carlcs 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 ...
- 171
5
votes
Unable to locate message source for category ''
In Craft 2, the Craft default Yii translation method was overridden to only require one value, the value of the translatable string:
Craft::t('My Translatable String');
In Craft 3, Craft reverts to ...
- 13.3k
5
votes
Localized ElementTypes in a plugin
The part that I'm not "getting" is I think there should be one Element in craft_elements and then for each localization, there should be one record in craft_elements_i18n yes?
Yes.
And then for ...
- 34k
5
votes
Accepted
How can I localize a URL in a plugin?
You can use UrlHelper::getSiteUrl() instead, which does have a $localeId argument, for generating a URL relative to a given locale’s base URL (per the siteUrl config setting).
So if your siteUrl ...
- 34k
5
votes
Accepted
Retrieve entry field value from a different locale?
You can just manually fetch the entry in the other locale. Assuming you already have the first entry, something like:
{% set localeEntry = craft.entries.id(entry.id).locale(languageID1).first() %}
{...
- 67k
5
votes
Accepted
Getting all entries from all locales
In my experience this has been problematic - instead have looped through locales to fetch entries then merged and sorted with supersort.
Eg:
{% set locales = craft.i18n.getSiteLocales %}
{% set ...
- 1,741
5
votes
Accepted
Multi-lingual site with a splash (language select screen) at the root
Well that was easy enough. I had forgotten about the .htaccess in the subfolders. Everything works like planned now.
public/.htaccess (redirects to craft.php if file doesn't exist)
public/index....
- 472
5
votes
How can I get the locales that are actually enabled for a given entry?
Actually, entry.getLocales() (or just entry.locales) will return an array of the locales enabled for the entry's section, not all locales in the system.
Unfortunately, there's no easy way to ...
- 21.7k
5
votes
Accepted
Passing JSON to Twig
The answer is to create a custom plugin that will read the json files from the folders in the public directory, parse the json, and pull them into the template.
An alternative solution is to use ...
- 791
5
votes
Accepted
Possible to change locale in PHP?
I think I got this resolved the proper way... looks like you just need to make sure that your AJAX calls are localized.
In my main _layout template:
<script type="text/javascript">
window....
- 23.8k
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
localization × 405craft3 × 52
multi-site × 48
language × 31
templating × 22
plugin-development × 21
entries × 20
url × 18
assets × 15
matrix × 14
multi-environment × 12
control-panel × 11
config × 11
entry × 10
datetime × 10
categories × 7
users × 7
sections × 7
navigation × 7
globals × 7
multi-domain × 7
craft2 × 6
routes × 5
plugin-feedme × 5
craft-pro × 5