in my theme I want to define a series of custom post types and custom taxonomies, each one having its own customized slug; the base language of my theme is english, therefore the slugs will be in English language
for example while defining the slug of custom post type “product” args:
'rewrite' => array( 'slug' => 'product' ),
is there any way to translate the “slug” through po/mo files?
can I put it as:
'rewrite' => array( 'slug' => __('product', 'mytextdomain') )
or it won’t work? what’s the current practice to localize slugs?
I wouldn’t try to localize your slugs. Instead, why not give your users the option to change them by adding another field to the permalink settings page?
Hook into
load-options-permalink.php
and set up some things to catch the$_POST
data to save your slug. Also add a settings field to the page.Then the call back function for the settings field:
Then when you register your post type, grab the slug with
get_option
. If it’s not there, use your default.Here’s the settings field portion as a plugin https://gist.github.com/1275867
EDIT: Another Option
You could also change the slug based on what’s defined in the
WPLANG
constant.Just write a quick function that holds data…
Then get the slug where you register your custom post type.
The best option, IMO, would be to both give the user an option and provide solid defaults:
I am doing exactly that in a theme we are developing. It is available in 5 distinct languages, and each language has a translated set of categories. The first component of the URL in the theme is parsed to determine which language is used, in country-language format:
And then translated categories are parsed as further components of the URL.
The URL is parsed in the
parse_request
phase:This example is devoid of requisite checks, but is meant only as an example.
There are drawbacks to this approach, of course, but it allows natural URLs in all languages. The main drawbacks I see are:
1) It doesn’t make use of the permalink mechanism. This could likely be extended so that the proper permalink rules for all languages are generated and parse_request won’t be necessary, but to do it for all of the languages would involve loading one MO file after another in a loop, and I don’t know how well supported that is.
2) If a translator changes a slug, then the links get invalidated.
If that doest not work Why not you just simple do:
You could try this in your
functions.php
as seen here
I actually had this problem, here’s my solution, which works when you know the languages of your website beforehand. Let’s say you have a CPT called “movie”, and your website has 3 languages. You need to rewrite your CPT permalink, for each language. You could do this dynamically too.
Now you just need to rebuild your permalinks: go to Settings > Permalinks and just press “Save”
The next part is rewriting URLs for your CPT, if you have a translated CPT you need to get the language of the post, in my case I use Polylang. It’s important to encode foreign characters in URLs
I would recommend not making slugs translatable.
Translation is for user-facing site content. Slugs are used internally, and are only marginally “public-facing” via URL rewrites – and URLs should not be translatable, either.
So: leave your slugs alone, as you define them. Only make translatable strings that are intended for public consumption.