Localization
Localization is an important aspect of any Craft CMS project, allowing you to provide a more personalized experience for your users by offering your content in multiple languages. This guide will help you understand how to localize your store using the YSCP plugin.
Adding Translation Dictionaries
Your store can be localized like any other Craft CMS theme. This is done by adding PHP translation dictionaries in a translations/ subfolder of your project root.
Creating Translation Files
To create translation files for your project:
-
Navigate to the translations folder: Ensure you have a
translationsfolder in your project root. If it doesn't exist, create it. -
Create language subfolders: Within the
translationsfolder, create subfolders for each language you want to support. For example, to supportEnglishandGerman, you would create the following structure:
root-directory
├── translations/
│ ├── en/
│ ├── de/
- Add translation files:
Inside each language subfolder, create a PHP file for the translation category. For the YSCP plugin we are using the
yui.phpfilename. Here’s an example structure:
### Example Translation File
<?php
return [
'welcomeMessage' => 'Welcome to our store!',
'productTitle' => 'Product Title',
'addToCart' => 'Add to Cart',
// Add more translations as needed
];
Plugin Translation Files
Our plugin uses the yui category for Craft CMS translations. This means that all translation strings in the plugin are referenced under this category.
Using Translation Strings
To use these translation strings in your files, you can use the Craft::t() function. Here’s an example:
- Twig
- PHP
{{ 'welcomeMessage'|t('yui', {params...}) }}
Craft::t('yui', 'welcomeMessage', [params...])
This will output the localized string for the welcomeMessage key from the yui category.
Supported Languages
YSCP currently supports the following languages:
- sk: Slovak
- cs: Czech
- de: German
- hu: Hungarian
- en: English
Adding New Languages
If you need to add support for a new language:
- Create a new subfolder for the language in the
translationsdirectory. - Add the yui.php file with the necessary translations.
For example, to add French support, you would create:
root-directory
├── translations/
│ ├── fr/
│ │ ├── yui.php
And populate the yui.php file with the appropriate translations.
Conclusion
By following these steps, you can easily localize your store and provide a better user experience for your international customers. If you have any questions or run into issues, please refer to the Craft CMS Localization Documentation for more detailed information.