It is not recommended to use t() for translating dynamic data. This way is quite insecure and not flexible.
For now it looks like this (title Article %node:title (%node:comment-count)):
translation with t()
It will be better, if we do t() before token replacement, but still not so good for maintenance.

Inspired by my first code contribution, I suggest a concept patch with “path_breadcrumbs_i18n” module. It uses i18n_string Framework for creation, updating and deleting i18n strings.

Important notes:

  • To interact with main module there is an alter hook 'path_breadcrumbs_build_breadcrumbs', but can be used another way.
  • To make i18n “Translate” tab visible I have created MENU_DEFAULT_LOCAL_TASK item (temporary solution).

Status of this patch:
Concept, needs review.

Further work:

  • UI improvement for i18n page
  • Rethinking 'translate' property
  • Appropriate translation of “Home” link (i18n_variable)
  • Testing, of course

How to test:
Apply a patch, enable “path_breadcrumbs_i18n” module, go to page admin/structure/path-breadcrumbs/edit/%/translate for any path breadcrumb with translatable flag set to 1.

Comments

spleshka’s picture

Wow, that is so cool, thank you indeed! I have made a quick code review and I really liked it. I will test this module as soon as I have some free time. Hope that it will be today =)

spleshka’s picture

You know, I don't like only one thing: now breadcrumbs will be translatable only if user enable i18n. I'm not sure that this is a good idea. Moreover, what will happen with current translations for breadcrumbs? I use translations for breadcrumbs on one project. If I just update to a new version they will gone and users will see untranslated content. We should think about migration.

kalabro’s picture

In defense of i18n_string
Yes, this solution has dependence i18n_string. Any complex multilingual Drupal 7 site has to use i18n package for menus, blocks, views, etc. i18n_string provides very clear infrastructure for storing multilingual data. I see i18n Views and i18n Rules uses i18n_string successfuly. Entity API began to provide i18n stuff for entities and this tendency is growing for entity modules. I believe i18n_string is the optimal solution for Drupal 7. For one-language sites this module can be disabled and some help message about how to turn on translation can be shown.
On the other hand, we can just t() them all — http://hojtsy.hu/blog/2011-may-19/drupals-multilingual-problem-why-t-wro....

Migration
It can be done, I think. For example, we can go through all path_breadcrumbs objects in hook_update or migration module and prepopulate i18n strings with old t() values.
But I'm not sure if it is really needed.

spleshka’s picture

I didn't say that i18n in bad solution. It is great solution! But I am worry about simple multilanguage sites. But I agree with you - we could make this module like Views: translations only with i18n.

Migration
I insists on migration. I showed you an example with my project: if I update to a new module version all my translations will disapear and I will have to create new translations. My project contains 84 breadcrumb navigations and ~20-30k visitors daily. I think that they are won't be happy to see unknown language on the screen.

spleshka’s picture

StatusFileSize
new8.64 KB

After last module updates I refactored your patch so now it is satisfies current requirements. Patch is attached.

spleshka’s picture

Status: Needs review » Needs work

I really don't like this code:

+++ b/path_breadcrumbs_i18n/path_breadcrumbs_i18n.module
@@ -0,0 +1,107 @@
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function path_breadcrumbs_i18n_form_path_breadcrumbs_ui_edit_form_alter(&$form, &$form_state) {
+  // Add submit function for PATH BREADCRUMBS EDIT form.
+  $form['#submit'][] = 'path_breadcrumbs_i18n_edit_form_submit';
+}
+
+/**
+ * Submit handler after saving path breadcrumbs.
+ *
+ * @param $form
+ * @param $form_state
+ */
+function path_breadcrumbs_i18n_edit_form_submit($form, &$form_state) {
+  $values = $form_state['values'];
+  if (isset($values['update_save']) && $values['op'] == $values['update_save']) {
+    $path_breadcrumbs = path_breadcrumbs_load_by_name($form_state['storage']['machine_name']);
+    if ($path_breadcrumbs) {
+      if ($path_breadcrumbs->translatable) {
+        i18n_string_object_update('path_breadcrumbs', $path_breadcrumbs);
+      } else {
+        i18n_string_object_remove('path_breadcrumbs', $path_breadcrumbs);
+      }
+    }
+  }
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function path_breadcrumbs_i18n_form_path_breadcrumbs_ui_delete_form_alter(&$form, &$form_state) {
+  // Add submit function for before breadcrumb is deleted.
+  array_unshift($form['#submit'], 'path_breadcrumbs_i18n_delete_form_submit');
+}
+
+/**
+ * Submit handler before deleting path breadcrumbs.
+ *
+ * @param $form
+ * @param $form_state
+ */
+function path_breadcrumbs_i18n_delete_form_submit($form, &$form_state) {
+  $values = $form_state['values'];
+  if ($values['op'] == $values['submit']) {
+    $path_breadcrumbs = path_breadcrumbs_load($values['path_id']);
+    i18n_string_object_remove('path_breadcrumbs', $path_breadcrumbs);
+  }
+}

I think that it is better to provide hook_path_breadcrumbs_save() and hook_path_breadcrumbs_delete() than try to change forms behavior. Your variant is not module-friendly: other modules will not get translate features if they will save/update/delete path breadcrumbs.

Now we got next todo list:

  • Refactor code to implement translations more carefully
  • Add migration from older module releases
kalabro’s picture

Really happy you suggested to create API!
I think we can provide hooks first (issue #1648260: Provide API for other modules) and than continue work with i18n.

Thanks for review. I will work on it tonight :)

spleshka’s picture

It is a really good idea to implement hooks first. I would be so happy to review patch with it :)

kalabro’s picture

StatusFileSize
new20.28 KB
new12.72 KB

Main changes since last patch:

  • Added fallback to t() if 'path_breadcrumbs_i18n' disabled
  • Added Variable support for Home link translation (see attached screenshot)
  • Translate link added to dropbutton links
kalabro’s picture

Status: Needs work » Needs review
kalabro’s picture

StatusFileSize
new567 bytes
new13.12 KB

Oops, forgot to remove the old piece of code :)

spleshka’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev
Assigned: Unassigned » spleshka
Status: Needs review » Fixed

Oh, I'm sorry that you had to wait so long. I'm finally got some time to review this patch.

I found there some bugs, fixed them and commited it to 7.x-3.x. I hope that new 3.x branch will bring to Path breadcrumbs new features (like full token support) and performance improvements.

Your patch fix my fixes was commited at 26b8bbf.

@kalabro, thank you so much for your time. I am really appreciate that.

kalabro’s picture

Thanks, @Spleshka!

Some notes:
To translate Home link title (if you use option “Prepend Home link to the breadcrumb”) you should:
- enable Variable translation module (i18n_variable) from Internationalization package
- make variable “Home link title” multilingual on page admin/config/regional/i18n/variable
- go to Path Breadcrumbs settings page (admin/structure/path-breadcrumbs/settings and set variable value for each language.

spleshka’s picture

I know, we should write documentation about it.

Automatically closed -- issue fixed for 2 weeks with no activity.