using GetText to translate wordpress using variables instead of default language - wordpress

I want to make my wordpress theme translation ready.
I was thinking to use GetText function within wordpress like: _e('sometext')
But I was thinking, what happens when I want to change default English 'sometext', I have to go to different php files (where I used _e('sometext')) find all instances and replace it with _e('sometext2') ?
Or in my PO file I just make English column, use _e('sometext') in wp php files, but in PO file specify 'sometext2'?

I would use something similar to the concept of constants, e.g:
<?php _e('TXT_ABOUT_INTO'); ?>
TXT_ABOUT_INTO will be like a placeholder, and you will need to create a translation file for English as well as for other languages.

Yes, if you decided to replace "sometext" with "some other text" you would need to go through the templates wherever _e('sometext') appeared and replace it with _e('some other text').
But... imagine you did not wrap your text in gettext calls. Then you would have to go through your templates to replace "sometext" with "some other text".
Not a lot of difference, and if you were to do a global find & replace you'd be much less likely to accidently change something if your search term was "_e('sometext')" than "sometext".
I've followed your example, but you should be including the text domain when you wrap the text, e.g. _e('sometext', 'my-theme')

Related

Translation files of my plugin are not being loaded or work incorrect

I need to translate some parts of text to French.
Things I've tried so far:
I created fr.po and fr.mo files in my plugin's /languages directory. I used Poedit for this purpose. I've tried different variants like fr_FR - didn't help.
I added the following to my plugin's main file along with its name and other information:
* Text Domain: pluginname
* Domain Path: /languages
Plugin name does not contain any special characters or underscores/dashes - it's a single word.
Also, tried to use load_plugin_textdomain() function instead (or even along with) to make this work.
Also, tried to add this to my wp-config.php file:
define ('WPLANG', 'fr_FR');
Tried to use any combinations of described actions as well.
I have a string to be translated:
__('Recently', 'pluginname')
The word "Recently" is being displayed correctly but it is not being translated if I change site language. I tried both changing in WP admin panel and adding into config file (mentioned above)
I tried to use get_locale() to check if this was actually changed. This returns 'fr_FR' which is exactly the same with my .po/.mo file names.
**P.S.: ** Checked all these questions and tried all suggested solutions - didn't help:
Wordpress - Plugin translation not working
WordPress plugin translation issue
Wordpress plugins translation
Update: load_plugin_textdomain() returns false if I try to var_dump() result right after function execution.
Actually, the problem was in the names of files. Other than locale name it should also consist of the plugin name, e.g. pluginname-fr_FR.po/pluginname-fr_FR.mo for my case. Yes, this is described in the codex, I should read this more attentively :)

how can i auto-register strings with wpml in wordpress?

i am trying to auto register string in wordpress, but for some reason it doesn't work
it works when i call in the code of the theme:
esc_html_e('mytext','mythemename');
and then do
WPML->theme and plugins localization->strings in the theme->scan the theme for strings
then mytext appears in strings list for translation
but if i try instead to enable:
wpml->string translation->auto register strings for translation->auto-register strings always->apply
and in the code run, f.ex.:
$myvar='mytext';
esc_html_e($myvar,'mythemename');
then, of course, i go to wp page where string appears, but nothing happens, string doesn't get added to theme strings list
does anybody know if i am doing something wrong?
it started adding the strings when instead of esc_html_e i wrote:
icl_register_string('mythemename','myvariable',$myvar);
echo icl_t('mythemename','myvariable',$myvar );
i'm sure there is more elegant solution but since this one works it'll do for now.

Migration to new domain

I'm working on a drupal 6 site at mydomain.com/drupalsite, and the designer has put a lot of hardcoded image paths in there, for instance a custom img folder in mydomain.com/drupalsite/img. So a lot of the site uses links to /drupalsite/img/myimg1.png.
Here's the problem -- the site is eventually moving to finaldomain.com, via pointing finaldomain.com to mydomain.com/drupalsite. So now paths like /drupalsite/img/myimg1.png will resolve to finaldomain.com/drupalsite/img/myimg1.png, instead of what should be finaldomain.com/img/myimg1.png. The finaldomain.com site has to point to that subdirectory so it hits the index.php.
My first instinct is to use an .htaccess file to replace the /drupalsite with "", but I've tried about a dozen different solutions and they haven't worked. My hack of a solution was to use some ln -s links but I really don't like it :) tia
Andrew
The best method, in hindsight, is to ensure folks use Drupal functions to make all links:
l (that's the letter L)
drupal_get_path()
base_path()
The l() function takes care of base path worries, and provides a systematic way to define your URL's. Using things like theme_image() plus the l() function are a sure win. Use the second and third functions above if you have to write your own <a> tags and for use inside theme functions like theme_image().
But for your current situation:
As regards Andy's solution, it would be better if you could limit your changes to certain database fields where you know the links are located.
So write a query to select all those fields (e.g. all body fields):
$my_query = db_query("SELECT vid, body FROM {node_revisions}");
This, for example, will get you every body field in the node_revisions table, so even your old revisions would have proper links.
Then run through those results, do str_replace() on each, and then write the changes back:
while($node = db_fetch_object($my_query)) {
$new_body = str_replace('what you have', 'what you want', $node->body);
db_query("UPDATE {node_revisions} SET body = '%s' WHERE vid = %d", $new_body, $node->vid);
}
I'd obviously try it on one record first, to make sure your code behaves as intended (just add a WHERE vid = 5, for example, to narrow it down to one revision). Furthermore, I haven't taken advantage of node_load and node_save, which are better for loading and saving nodes properly, so as to provide a more general solution (for you to replace text in blocks, etc.).
For your files, I'd suggest a good ol' sed command, by running something like the following from within your "sites" folder:
find ./ -type f -exec sed -i ’s/string1/string2/’ {} \;
Nabbed that from here, so take a look on that site for more explanation. If you're going to be working with paths, you'll either need to escape the / of the paths in your version of the sed command, or use a different sed separator (i.e. you can write s#string1#string2# instead of s/string1/string2/, so you could write s#/drupalsite/img/#/img# instead of s/\/drupalsite\/img\//\/img/ :-). See also Drupal handbook page for quick sed commands: http://drupal.org/node/128513.
A bit of a mess, which is why I try to enforce using the proper functions up front. But this is difficult if you want themers to create Drupal content but you don't want to give them access to the "PHP Filter" input format, or they simply don't know PHP. Proper Drupal theming, at any point past basic HTML/CSS work, requires a knowledge of PHP and Drupal's theme-related functions.
I've done this before by taking a full database dump, opening it in a text editor, and doing a global search and replace on the paths. Then on the new host, load the modified dump file, and it will have the correct paths in.
You could try Pathologic, it should be able to correct paths like this.

Drupal best way to change text

I'm using the module "simplenews" and during the registration process it reads "Select the newsletter(s) to which you wish to subscribe.".
I'd like to change this line and wanted to ask what the best way is to do so without hard coding it?
Thanks in advance!
I've also done this with the custom english version just so I could override a few strings. It works and is easier than hook_form_alter, but it also creates a bit of unnecessary overhead, especially if your site is otherwise not localized/multilingual.
There is nowadays a light and easy solution, that uses the same locale system, but you don't need to setup a custom english language or code anything: the "String Overrides"-module.
http://drupalmodules.com/module/string-overrides
Surprisingly, it's one of the top rated modules, out of over 5000 module.
Since it's located in a form, an easy and quick solution would be to change it with hook_form_alter.
Another solution would be to create your own custom version of the language english in Drupal and then "translate" the string into something different.
You could also skip the translation part, and use your settings.php file. You can create some custom string override in it without enableling the locale module. From the default.settings.php file:
/**
* String overrides:
*
* To override specific strings on your site with or without enabling locale
* module, add an entry to this list. This functionality allows you to change
* a small number of your site's default English language interface strings.
*
* Remove the leading hash signs to enable.
*/
# $conf['locale_custom_strings_en'] = array(
# 'forum' => 'Discussion board',
# '#count min' => '#count minutes',
# );
Or you can use String Overrides. Some more details about this module (from its project page):
Provides a quick and easy way to replace any text on the site.
Features
Easily replace anything that's passed through t()
Locale support, allowing you to override strings in any language
Ability to import/export *.po files, for easy migration from the Locale module
Note that this is not a replacement to Locale as having thousands of overrides can cause more pain then benefit. Use this only if you need a few easy text changes.

Changing the QTY label in Uber Cart?

How do you change the QTY (quantity) label in UberCart (in Drupal) without actually hacking the core components? I want the label to be be months, instead of qty.
You could use the String Overrides module. Here is an excerpt from its project page:
Provides a quick and easy way to replace any text on the site.
Features:
Easily replace anything that's passed through t()
Locale support, allowing you to override strings in any language
Ability to import/export *.po files, for easy migration from the Locale module
Note that this is not a replacement to Locale as having thousands of overrides can cause more pain then benefit. Use this only if you need a few easy text changes.
I once ran into a similar issue with Ubercart in another language (German), and we "solved" it by re-translating the string. The mentioned module should do the trick in your case.
I haven't used ubercarts, but I would guess there would be an admin section to do that. Else hook_form_alter() or hook_form_FORM_ID_alter() should be able to do the trick for you.
Unfortunately, there is no setting for this in ubercart.
Doing a search for 'Qty' (case sensitive, as there are numerous 'qty' in code) in the current ubercart-6.x-2.0-rc7 release gives seven matches:
3 in theme functions, which you'd need to override in your theme
1 in a form definition, which you'd need to change via hook_form_alter as googletorp suggested
3 in table definitions, which you'd need to change via hook_tapir_table_alter and/or hook_tapir_table_header_alter (see the hooks.php file in ubercarts doc directory for these)
So you should be able to implement your changes without changing the module itself, but given the amount of work involved, I'd try schnecks suggestion first ;)

Resources