I’ve taken over a dual language Woocommerce site, State/Counties have been modified with custom counties but they have not been translated.
How do I go about this
Here’s the code from functions.php, it looks like it’s an adaptation of code from BBloomers website.
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function bbloomer_custom_woocommerce_states() {
global $states;
$states['IE'] = array(
'AN' => 'Antrim',
'AR' => 'Armagh',
'CE' => 'Clare',
'CN' => 'Cavan',
'CO' => 'Cork',
'CW' => 'Carlow',
'D]' => 'Dublin',
'DL' => 'Donegal',
'DW' => 'Down',
'FE' => 'Fermanagh',
'G]' => 'Galway',
'KE' => 'Kildare',
'KK' => 'Kilkenny',
'KY' => 'Kerry',
'LD' => 'Longford',
'LH' => 'Louth',
'LK' => 'Limerick',
'LM' => 'Leitrim',
'LO' => 'Derry',
'LS' => 'Laois',
'MH' => 'Meath',
'MN' => 'Monaghan',
'MO' => 'Mayo',
'OY' => 'Offaly',
'RN' => 'Roscommon',
'SO' => 'Sligo',
'TY' => 'Tipperary',
'TE' => 'Tyrone',
'WD' => 'Waterford',
'WH' => 'Westmeath',
'WW' => 'Wicklow',
'WX' => 'Wexford',
);
return $states;
}
Is it possible to replace these with strings so that I can use WPML to translate or do I need to hard code again in the functions file?
2
Answers
To make any text string translatable, you will need to use specific
__()
WordPress function, for each text string (state).Also in your code, the
$states
variable is already available as an argument from the function when usingwoocommerce_states
so incorrectglobal $states;
is not really needed (see this official code snippet).So your code will be (set your active theme’s text domain inside the function):
Related tread: Add states for shipping zones instead of postcodes in Woocommerce
For info: Bloomers code is taken from official WooCommerce Add/Modify States in WooCommerce…
I would also recommend a read through http://ottopress.com/2012/internationalization-youre-probably-doing-it-wrong/
Reproduced here very briefly:
It’s an older article, but helps explain the why and what-is-this-doing of translation.