i am developing web app with ZF2. It is a multilingual website. I would like to use ZF2 Translator for translation. Because, admin can modify the language variables. So, i planned to use the language folder files. I will update the language files while the admin modifying the language variables. Can anyone suggest ideas for this implementation? Thanks.
Translating based on user input can get a little messy. There is no easy way of doing that right now, as the currently supported formats are all file formats: php files with arrays, a gettext .po file or Tmx/Xliff files. For the easiest access, you would likely store the translations in a database.
You therefore need to write your own loader, loading the translations from the database. An alternative is you will write the translations in a php array and export that to a file, but I would not recommend that. For the database loader, you must implement the RemoteLoaderInterface. In very simple terms:
use My\DbTable;
use Zend\I18n\Translator\Loader\RemoteLoaderInterface;
Zend\I18n\Translator\TextDoamin;
class DbLoader implements RemoteLoaderInterface
{
protected $table;
public function __construct(DbTable $table)
{
$this->table = $table;
}
public function load($locale, $textDomain)
{
$data = $this->table->loadFromLocale($locale);
// process $data into $messages
// $messages is array(key=>value) with key translation key
$domain = new TextDomain($messages);
return $domain;
}
}
The db table loads the messages in the locale you specify. If you use text domains, pass that on too. Then you need to configure your Translator such it uses this loader too to load the message. Everything else should be fine then. In your views, you can create translations like this:
<?php echo $this->translate('my original text'); ?>
The SkeletonApp comes with Translation support built-in. However further details on how to use it can also be found in the manual -> http://zf2.readthedocs.org/en/latest/modules/zend.i18n.translating.html
I personally keep all my language files in a folder called /language
In your views you could also simply call
$this->translate('text_you_want_to_translate');
Related
Using ZfcTwig for ZF2 and twig-gettext-extractor, I still cannot extract messages for translation from twig-files by poedit. I works if I used the formal twig keyword for translation {% trans(MY_TEXT) %} but not for the in-built view helper translate. {{ translate(MY_Text) }} does the translation but poedit is just ignoring it. For new twig files, I want as usual let poedit do the job....
Any ideas for a solution?
Maybe you need to edit catalog properties keywords to be visible for translations. Open PoEdit, go to Catalog -> Properties -> Sources keywords and add another keyword "translate". Here I attach an screenshot.
The problem is that the extractor you are using is just caching the files and running them through xgettext to extract calls to trans/translate/_/.. (keywords as suggested by Conti. Alas ZfcTwig will crosscompile calls to ZF2 plugins into plugin('translate')->__invoke('Your Text to be translated'). You could of course now add __invoke as a new keyword in poedit or whatever you favorite gui for using xgettext is, but it will now find all calls to all view helpers not just those to translate.
I ran into this problem myself and I have not come up with a satisfying solution. The Twig Gettext Extension looks promising in terms of writing your own customized POT-File generator that will handle translate and translatePlural view helper calls. Using the extension as is will bypass all translation from ZF2. ZF2 parses .mo files into an internal structure rather than using the php-gettext mod.
All in all I gave up on automated po(t) file generation for ZF2+ZfcTwig for now and am back to phpArray.
So just to give you some context, I'm trying to create a generator that will create some files (based on user input of course) as well as update some existing files in the project (like adding a new route for example).
Creating the files using this.template is no problem... the question is: is there any way to do this with Yeoman without having to read the file using Node and doing some fanciful find and replace?
Ok, so I found the answer to my question.
Addy Osmani showed me where to look in this thread on twitter, and then I later found this link which shows exactly what I need.
The gist of it boils down to two functions : readFileAsString and write. Usage is as follows:
var path = "/path/to/file.html",
file = this.readFileAsString(path);
/* make modifications to the file string here */
this.write(path, file);
Edit: I've also blogged about this on my blog.
EDIT 1
As mentionned in comments by Toilal :
The write method doesn't exists anymore, and must be replaced by writeFileFromString (arguments are also reversed) – Toilal
EDIT 2
And then, as mentionned in comments by ivoba:
this.writeFileFromString & this.readFileAsString are deprecated, github.com/yeoman/html-wiring should be used by now, things change :) – ivoba
Yeoman also provides a more elegant way of fs operations using mem-fs-editor.
You can use this.fs.copy, passing a process function as an option to do any modifications to the content:
this.fs.copy(path, newPath, {
process: function(content) {
/* Any modification goes here. Note that contents is a Buffer object */
var regEx = new RegExp('old string', 'g');
var newContent = content.toString().replace(regEx, 'new string');
return newContent;
}
});
This way you can also take advantage of mem-fs-editor features.
In combination with #sepans' excellent answer, instead of using regular expressions, one can use some parser.
From Yeoman documentation:
Tip: Update existing file's content
Updating a pre-existing file is not always a simple task. The most reliable way to do so is to parse the file AST and edit it. The main issue with this solution is that editing an AST can be verbose and a bit hard to grasp.
Some popular AST parsers are:
Cheerio for parsing HTML.
Esprima for parsing JavaScript - you might be interested in AST-Query which provide a lower level API to edit Esprima syntax tree.
For JSON files, you can use the native JSON object methods.
Parsing a code file with RegEx is perilous path, and before doing so, you should read this CS anthropological answers and grasp the flaws of RegEx parsing. If you do choose to edit existing files using RegEx rather than AST tree, please be careful and provide complete unit tests. - Please please, don't break your users' code.
More specifically, when using esprima you will most probably require also some generator such as escodegen to generate js back.
var templatePath = this.destinationPath(...);
this.fs.copy(templatePath, templatePath, {
process: function (content) {
// here use the parser
return ...
}
});
Note that you can use the same path in from, to arguments in order to replace the existing file.
On the other hand, the downside of such parsers is that in some cases it may alter the original file way too much, and although safe, this is more intrusive.
Use var text = this.fs.read(filePath) to read from a file and this.fs.write(filePath, content) to write to a file at location.
You can use the mem-fs-editor, and call the fs.append(filepath, contents, [options]) method.
e.g.
this.fs.append(this.contextRoot + "/index.html", " <p>Appended text</p>");
I'm building an ASP.Net MVC4 application and the customer wants to be able to supply an XML configuration file, to configure a vendor list in the application, something like this:
<Vendor>
<Vendor name="ABC Computers" deliveryDays="10"/>
<Vendor name="XYZ Computers" deliveryDays="15"/>
</Vendors>
The file needs to be dropped onto a network location (i.e. not on the web server) and I don't have a database to import and store the data.
The customer also wants the ability to update it daily. So I'm thinking I'll have to do some kind of import (and validate the file) when the application starts up.
Any good ideas on the best way to accomplish this?
- The data needs to be quickly accessible
- Ideally I just want to import/store it once, or be able to access it quickly
- I need to be able to validate the file, so it might be prudent to be able to be able to switch to a backup
One thought was to use something like Entity Framework and simply read the file whenever I needed it, but if possible I'd hold it in memory in the application if possible.
Cheers
Vincent
No need to import it into a database or use Entity Framework. You can simply use .NET Xml Serialization to accomplish this.
The command line tool xsd.exe will generate c# classes from your Xml file. From the command line:
xsd.exe myfile.xml
xsd.exe /c myfile.xsd
The first command will infer and create an xml schema file (myfile.xsd) from your xml. The second command will convert the schema file to c# classes.
Then use the XmlSerializer class to deserialize your xml file into objects (assuming multiple objects in one file):
MyCollection myObjects= null;
string path = "mydata.xml";
XmlSerializer serializer = new XmlSerializer(typeof(MyCollection));
StreamReader reader = new StreamReader(path);
myObjects = (MyCollection)serializer.Deserialize(reader);
reader.Close();
You can use the .xsd file generated above to validate your xml files. Here's a link showing how: http://msdn.microsoft.com/en-us/library/ms162371.aspx.
I realise this breaks the MVC pattern, but there is a viable reason for doing it this way in an application I am currently building :)
What I am trying to do is output a JavaScript bundle directly from the Controller rather than via a link via a View.
So for example I have a bundle called "~/jQueryPlugin" what I'd like to do is something along the lines of
return this.JavaScript(BundleTable.GetBundle("~jQueryPlugin").BundleContent)"
However for the life of me I cannot figure out what the BundleTable.GetBundle("~jQueryPlugin").BundleContent part should be in order to get a string representation of the combined minimized bundle.
Any help would be appreciated·
In the 1.1-alpha1 release we added a new Optimizer class which should allow you to more easily do this. Its intended to be a standalone class that's useable out of side of ASP.NET hosting, so setting it up will be slightly different.
You can get the bundle contents out via something like this:
OptimizationSettings config = new OptimizationSettings() {
ApplicationPath = "<your physical path to the app>",
BundleSetupMethod = (bundles) => {
bundles.Add(new ScriptBundle("~/bundles/js").Include("~/scripts/jqueryPlugin.js"));
}
};
BundleResponse response = Optimizer.BuildBundle("~/bundles/js", config);
Assert.IsNotNull(response);
Assert.AreEqual("<your bundle js contents>", response.Content);
Assert.AreEqual(JsMinify.JsContentType, response.ContentType);
The next release should be fleshing this scenario out more, as it is needed for build time bundling integration with Visual Studio.
In GWT I have to specify what locales are supported in my application. The code get compiled in various files, one for each locale (beside other versions), but I have to give my clients one only URL. This URL is supposed to be a page that should be displayed according to the locale preferred by the browser.
I dont't want to have an HTTP parameter for the locale since I want to forse the locale preferred by the browser.
How can this be coded in GWT?
Should I try to to this using apache rewrite rules? I thied it, but I think I cannot access such parameter easely in a rewrite rule.
Thanks a lot,
Giuseppe
I had the same problem as you, but as I really need to know the current locale (I'm requesting a second server for data that I want to be localizable) I found this class:
com.google.gwt.i18n.client.LocaleInfo#getCurrentLocale(). That should give you what GWT uses currently.
GWT has good support for internationalization. See this link. The i18nCreator command can help you to set up the internationalization infrastructure for similar to the way projectCreator and applicationCreator set up the GWT application.
If you have static strings (i.e. Invalid Entry!) that need to be internationalized, you don't need any additional flag to i18nCreator command to create the properties files and infrastructure.
If you have strings that need to accept parameters (i.e. Hello {0}), you need to pass the -createMessages flag to i18nCreator command to create the properties files and infrastructure.
Now your module needs to include the i18n module in your MyApplication.gwt.xml:
<inherits name="com.google.gwt.i18n.I18N"/>
Define a Java interface in the same package as your property files that extends Constants or Messages and defines methods (name matches the property entries) that all return string.
MyConstants.properties contains:
errorMessage=Invalid Entry!
MyConstants.java contains:
import com.google.gwt.i18n.client.Constants;
public interface myConstants extends Constants {
String errorMessage();
}
Now to access these internationalized Strings from you application:
public class MyApplication implements EntryPoint {
private static final MyConstants constants = (MyConstants)GWT.create(MyConstants.class);
public void onModuleLoad() {
final Label errorMessage = new Label(constants.errorMessage);
}
}
GWT implements the interface for you automagically.
You can get messages in a similar way.
Hopefully this can help you get started.
Unless I am reading the documentation incorrectly I don't think you have to do anything.
GWT and Locale
By making locale a client property, the standard startup process in gwt.js chooses the appropriate localized version of an application, providing ease of use (it's easier than it might sound!), optimized performance, and minimum script size.
The way I read it, as long as your module has added all the locale choices to it, it should be automatic?
Check this com.google.gwt.i18n.client.LocaleInfo.getCurrentLocale()
<inherits name="com.google.gwt.i18n.I18N"/>
<!-- Use browser-specified locale for i18n -->
<set-configuration-property name="locale.useragent" value="Y"/>
<!-- Specify locales your application support -->
<extend-property name="locale" values="en"/>
<extend-property name="locale" values="de_DE"/>
<extend-property name="locale" values="ru_RU"/>