{"id":132,"date":"2012-09-13T15:28:44","date_gmt":"2012-09-13T14:28:44","guid":{"rendered":"http:\/\/bemused.org\/blog\/?p=132"},"modified":"2026-12-01T14:47:57","modified_gmt":"2026-12-01T13:47:57","slug":"symfony-i18n-dumping-and-loading-translation-strings","status":"publish","type":"post","link":"https:\/\/bemused.org\/blog\/2012\/09\/13\/symfony-i18n-dumping-and-loading-translation-strings\/","title":{"rendered":"Symfony i18n : dumping and loading translation strings"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I&#8217;ve been meaning to write about this for literally years, but never got round to it. Hopefully this will be useful to someone. After an only partially sucessful attempt to manage symfony i18n strings in a MySQL DB, I finally gave up and reverted back to the default XLIFF file method. One of the reasons for initially wanting to store the translations in a DB was to build an easy to use interface to allow translators to easily find untranslated strings. Unfortunatley this didn&#8217;t work as well as I was hoping, and the alternatives (installing pootle, or getting translators to install and learn how to use an xliff tool) werent&#8217; much better.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I finally decided that the easiest way to update translations would be by dumping untranslated strings to a file before sending them to a translator. Unfortunately I couldn&#8217;t find a way to easily dump strings to a file and reload translated strings, so I wrote a couple of new tasks to do this for me.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">You need to create the following two files: lib\/tasks\/sfI18nDumpTask.class.php<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\n&lt;?php\n\/*\n* Current known limitations:\n* - Can only works with the default \"messages\" catalogue\n* - For file backends (XLIFF and gettext), it only saves\/deletes strings in the \"most global\" file\n*\/\n\n\/**\n* Dumps i18n strings from messages.xml files.\n*\n*\/\nclass sfI18nDumpTask extends sfBaseTask\n{\n\/**\n* @see sfTask\n*\/\nprotected function configure()\n{\n$this-&gt;addArguments(array(\nnew sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),\nnew sfCommandArgument('culture', sfCommandArgument::REQUIRED, 'The target culture'),\n));\n\n$this-&gt;addOptions(array(\nnew sfCommandOption('untranslated', null, sfCommandOption::PARAMETER_NONE, 'Output all new found strings'),\n));\n\n$this-&gt;namespace = 'i18n';\n$this-&gt;name = 'dump';\n$this-&gt;briefDescription = 'dumps i18n strings from messages.xml files';\n\n$this-&gt;detailedDescription = &lt;&lt;&lt;EOF\nThe &#x5B;i18n:dump|INFO] task extracts i18n strings from your project files\nfor the given application and target culture:\n\n&#x5B;.\/symfony i18n:dump frontend fr|INFO]\n\nBy default, the task dumps all translations it found in the current project.\n\n&#x5B;.\/symfony i18n:dump frontend fr|INFO]\n\nIf you want to dump the untranslated strings, use the &#x5B;--untranslated|COMMENT] option:\n\n&#x5B;.\/symfony i18n:dump --untranslated frontend fr|INFO]\n\nEOF;\n}\n\npublic function execute($arguments = array(), $options = array())\n{\n$this-&gt;logSection('i18n', sprintf('dumping i18n strings for the \"%s\" application', $arguments&#x5B;'application']));\n\n\/\/ get i18n configuration from factories.yml\n$config = sfFactoryConfigHandler::getConfiguration($this-&gt;configuration-&gt;getConfigPaths('config\/factories.yml'));\n\n$class = $config&#x5B;'i18n']&#x5B;'class'];\n$params = $config&#x5B;'i18n']&#x5B;'param'];\nunset($params&#x5B;'cache']);\n\n$i18n = new $class($this-&gt;configuration, new sfNoCache(), $params);\n$i18n-&gt;getMessageSource()-&gt;setCulture($arguments&#x5B;'culture']);\n$i18n-&gt;getMessageSource()-&gt;load();\n\n$messages = $i18n-&gt;getMessageSource()-&gt;read();\n\n$count=0;\nforeach ($messages as $catalogue =&gt; $translations) {\n$this-&gt;logSection('i18n', sprintf('found \"%d\" i18n strings', count($translations)));\nforeach ($translations as $key =&gt; $values) {\nif ($options&#x5B;'untranslated'] &amp;&amp; ('' != $values&#x5B;0])) {\ncontinue;\n}\n$count++;\n$source = str_replace('\"', '\"\"', $key);\n$translation = str_replace('\"', '\"\"', $values&#x5B;0]);\nprint \"$values&#x5B;1]|\\\"$arguments&#x5B;culture]\\\"|\\\"$source\\\"|\\\"$translation\\\"\\n\";\n}\n}\n\n$this-&gt;logSection('i18n', sprintf('dumped \"%d\" i18n strings', $count));\n\n}\n}\n\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and lib\/task\/sfI18nLoadTask.class.php<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\n&lt;?php\n\n\/*\n* Current known limitations:\n* - Can only works with the default \"messages\" catalogue\n* - For file backends (XLIFF and gettext), it only saves\/deletes strings in the \"most global\" file\n*\/\n\n\/**\n* loads i18n strings from php files.\n*\n*\/\nclass sfI18nLoadTask extends sfBaseTask\n{\n\/**\n* @see sfTask\n*\/\nprotected function configure()\n{\n$this-&gt;addArguments(array(\nnew sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),\nnew sfCommandArgument('culture', sfCommandArgument::REQUIRED, 'The target culture'),\nnew sfCommandArgument('filename', sfCommandArgument::REQUIRED, 'The translation csv file'),\n));\n\n$this-&gt;addOptions(array(\nnew sfCommandOption('no-overwrite', null, sfCommandOption::PARAMETER_NONE, 'Dont overwrite existing translations'),\n));\n\n$this-&gt;namespace = 'i18n';\n$this-&gt;name = 'load';\n$this-&gt;briefDescription = 'loads i18n strings from php files';\n\n$this-&gt;detailedDescription = &lt;&lt;&lt;EOF\nThe &#x5B;i18n:load|INFO] task loads i18n strings from your project files\nfor the given application and target culture:\n\n&#x5B;.\/symfony i18n:load frontend fr filename|INFO]\n\nBy default, the task will load all strings from the file\ninto the current project.\n\nIf you only want to load previously untranslated strings, use the &#x5B;--no-overwrite|COMMENT] option:\n\n&#x5B;.\/symfony i18n:load --no-overwrite frontend fr filename|INFO]\n\nEOF;\n}\n\n\/**\n* @see sfTask\n*\/\npublic function execute($arguments = array(), $options = array())\n{\n$this-&gt;logSection('i18n', sprintf('loading i18n strings for the \"%s\" application', $arguments&#x5B;'application']));\n\n\/\/ get i18n configuration from factories.yml\n$config = sfFactoryConfigHandler::getConfiguration($this-&gt;configuration-&gt;getConfigPaths('config\/factories.yml'));\n\n$class = $config&#x5B;'i18n']&#x5B;'class'];\n$params = $config&#x5B;'i18n']&#x5B;'param'];\nunset($params&#x5B;'cache']);\n\n$i18n = new $class($this-&gt;configuration, new sfNoCache(), $params);\n$i18n-&gt;getMessageSource()-&gt;setCulture($arguments&#x5B;'culture']);\n$i18n-&gt;getMessageSource()-&gt;load();\n\n$messageSource = $i18n-&gt;getMessageSource();\n$messages = $messageSource-&gt;read();\n\nif ($loaded_list = file($arguments&#x5B;'filename'])) {\nforeach ($loaded_list as $line) {\n$line = chop($line);\nif (preg_match('\/(\\d+)\\|\"(.*)\"\\|\"(.*)\"\\|\"(.*)\"\/', $line, $matches)) {\nlist($id, $culture, $source, $translation) = array($matches&#x5B;1], $matches&#x5B;2], $matches&#x5B;3], $matches&#x5B;4]);\nif ($culture == $arguments&#x5B;'culture']) {\n$messageSource-&gt;update($source, $translation, '');\n}\n}\n}\n} else {\n$this-&gt;logSection('i18n', sprintf('Cant open file \"%s\"', $arguments&#x5B;'filename']));\n}\n\nif ($options&#x5B;'no-overwrite'])\n{\n$this-&gt;logSection('i18n', 'saving only new i18n strings');\n} else {\n}\n$messageSource-&gt;save();\n\n}\n}\n\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Before dumping the new strings to a file, you should first add any new untranslated strings using the builtin symfony i18n:extract command: <pre class=\"brush: bash; title: ; notranslate\" title=\"\"> .\/symfony i18n:extract --auto-save --display-new frontend &lt;culture&gt; <\/pre> Then you&#8217;re ready to dump the new untranslated strings to a file : <pre class=\"brush: bash; title: ; notranslate\" title=\"\"> .\/symfony i18n:dump frontend &lt;culture&gt; --untranslated &gt; untranslated.csv <\/pre> You can then import this file into your favourite spreadsheet program. In NeoOffice this is done by opening a fresh spreadsheet and importing the data via &#8220;Insert&#8221; &gt; &#8220;Sheet from external file&#8221; and making sure to add pipe &#8216;|&#8217; as field seperator. YMMV in your spreadsheet tool of choice. Once the translations have been added in the 4th column of the spreadsheet, save the document as a pipe separated csv file before using the following command to reload the translated strings: <pre class=\"brush: bash; title: ; notranslate\" title=\"\"> .\/symfony i18n:dump frontend &lt;culture&gt; translated.csv <\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been meaning to write about this for literally years, but never got round to it. Hopefully this will be useful to someone. After an only partially sucessful attempt to manage symfony i18n strings in a MySQL DB, I finally gave up and reverted back to the default XLIFF file method. One of the reasons [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[29],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-geeky-stuff","tag-symfony-translation-i18n-l10n"],"_links":{"self":[{"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":20,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":359,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/posts\/132\/revisions\/359"}],"wp:attachment":[{"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}