(mt) Media Temple的GS绑定域名别名方法

其实(mt) Media Temple 的(gs) Grid-Services是可以绑定域名别名的,只是(mt)的后台太过于简陋,没有提供可视化界面供操作。添加方法如下:
假设,需要将alt-example.com指向example.com所在目录

  1. 在帐户中心(AccountCenter)将要添加的域名作为alternate domain添加进去。
  2. 登录SSH,执行如下指令移除alt-example.com目录:
    cd domains
    rm -rf alt-example.com
  3. 创建符号链接将alt-example.com指向example.com
    ln -s example.com alt-example.com

这样子域名别名就设置好了,可以打开浏览器访问试试。
如果要移除符号链接,登录SSH执行如下指令:

rm -f alt-example.com

via (mt)
-EOF-

Google App Engine应用国际化(i18n)支持解决方案

这篇文章是由livibetter发布在blogspot上的,由于原文被墙,特转载过来,供参考。
(This post is written only for Django 0.96.1 in GAE)

Two days ago, I started to create another Google App Engine application. This application will be internationalized when it’s finished. I tried searching for some solution, then I realized that there is no very simple way to achieve.

Normally, you can handle gettext stuff on your own, but our Google App Engine applications usually use templating from the SDK, which is from Django actually. One way or another, we have to incooperate with Django partially.

The goal here is:

  • Use minimal Django stuff, only import the essential stuff in order to get Django’s I18N support to work.
  • Messages in template must be translated, too.
  • Capable to decide the language from the cookie, django_language, or the request header, HTTP_ACCEPT_LANGUAGE.

I have already made a sample code, which you can read here and you can see it at http://yjltest.appspot.com/i18n.

Before we go into the code, please read the I18N and Settings of Django.

Setting Up

We need to use Django Settings to make I18N work. The reason of using Setting was due to Django’s gettext helper will require Settings module and decide location of message files by the location of Settings module.

If we want to use Django Setting, we must run the following code:

1
2
3
4
5
6
from google.appengine.ext.webapp import template
 
os.environ['DJANGO_SETTINGS_MODULE'] = 'conf.settings'
from django.conf import settings
# Force Django to reload settings
settings._target = None

Note that you must import the google.appengine.ext.webapp.template module, or you might get error about conf.settings is not able to be imported.

We need to set the environment varible DJANGO_SETTINGS_MODULE to the location of Setting module, conf.settings in this case. conf is the package and settings is a module file, our Settings module.

Why conf? Because when we generate message files from Python scripts and templates — we will see how to generate later, the Django message file generator, make-messages.py, will create files under conf/locale/ from where it’s run.

Settings

What do we need in conf/settings.py?

1
2
3
4
5
6
7
8
9
10
11
12
USE_I18N = True
 
# Valid languages
LANGUAGES = (
    # 'en', 'zh_TW' match the directories in conf/locale/*
    ('en', _('English')),
    ('zh_TW', _('Chinese')),
    # or ('zh-tw', _('Chinese')), # But the directory must still be conf/locale/zh_TW
    )
 
# This is a default language
LANGUAGE_CODE = 'en'

Mark the messages

Wraps those need to be translated with _(“message”) in Python script and {% trans “message” %} in template files. Please read I18N for more usages.

Generate message files

Before you run the helper script, we need to create conf/locale, the helper won’t create it for us.

Make sure you are at root of Google App Engine application’s directory, then run

$ PYTHONPATH=/path/to/googleappengine/python/lib/django/ /path/to/googleappengine/python/lib/django/django/bin/make-messages.py -l en

/path/to/googleappengine/ is the Google App Engine SDK’s location. This command should generate the conf/locale/en/LC_MESSAGE/django.po. Now you can open it to translate.

Don’t forget to set CHARSET, Usually UTF-8 will be fine, the line would read like:

"Content-Type: text/plain; charset=UTF-8\n"

Once you finish translating, you need to run

$ PYTHONPATH=/path/to/python/googleappengine/lib/django/ /path/to/googleappengine/python/lib/django/django/bin/compile-messages.py

It will geneate django.mo files in each language directories. You also need to update when you modify scripts or template, run

$ PYTHONPATH=/path/to/googleappengine/python/lib/django/ /path/to/googleappengine/python/lib/django/django/bin/make-messages.py -a

This will update all languages in conf/locale.

Working?

If you run you application, now it should show the language in conf.settings.LANGUAGE_CODE.

This is a per application setting, which is not normally that we want. We will expect each user can choose their own language. Django has a helper that calls LocaleMiddleware can do the job, unfortunately, it needs Django’s request and response class to work normally.

Do the dirty job

In order to do what LocaleMiddleware does, we need to make Google App Engine’s request/response objects have same behavior as Djagno’s. For easing the complexity, we create a new class, I18NRequestHandler, which inherits google.ext.webapp.RequestHandler. You only need to replace with it in your handlers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import os
 
from google.appengine.ext import webapp
from django.utils import translation
 
class I18NRequestHandler(webapp.RequestHandler):
 
  def initialize(self, request, response):
 
    webapp.RequestHandler.initialize(self, request, response)
 
    self.request.COOKIES = Cookies(self)
    self.request.META = os.environ
    self.reset_language()
 
  def reset_language(self):
 
    # Decide the language from Cookies/Headers
    language = translation.get_language_from_request(self.request)
    translation.activate(language)
    self.request.LANGUAGE_CODE = translation.get_language()
 
    # Set headers in response
    self.response.headers['Content-Language'] = translation.get_language()
#    translation.deactivate()

Where Cookies is from http://appengine-cookbook.appspot.com/recipe/a-simple-cookie-class/. When request comes in, it can automatically activate the language from what Cookies/Headers specify.

Caching problem

It’s not so perfect. I have noticed a problem in development server. If you change code and/or the message file, recompile the message file while server still runs, those message in entry script may not be translated for reflecting to cookie django_language’s change. I believe that is about the caching.

I am not sure the natural problems, so I couldn’t solve it. However, this may not be severe problem.

Encoding

If you use unicode string (not str string) in {% blocktrans %} template tag, you may get error, encode it to utf-8 first, e.g. s.encode(‘utf-8’).

Language Code

You must use underscore not dash for messages directory, e.g aa_BB, or Django would not recognize directory named as aa-BB or aa-bb. But in conf.settings you can use aa-bb, this means the language code and directory can be different, e.g. zh-tw for the language code in Python and zh_TW as message directory name.

Conclusion

Although this will work, but it may be broken if any changes to Django framework within Google App Engine. There isn’t a good solution for I18N in Google App Engine if Google doesn’t natively support it.

Updated on 2009-11-25: Added not about template module first and encoding issue, and updated the path of Python lib in GAE SDK.
Updated on 2009-12-24: Added a note about Language Code format, thanks BRAGA, again.
Updated on 2010-02-04: Added a note about the Language Code and message directory name.
Posted by livibetter at 2/25/2009 03:42:00 PM

-EOF-

Visual Studio 2010 试用版ISO下载

Visual Studio 2010放出的试用版目前都还是英文的,下载地址如下:
http://www.microsoft.com/visualstudio/zh-cn/download
相信不久后就会有中文版提出,微软默认给的是几个分解的压缩包,实际仍提供的有ISO镜像文件直接下载,该链接放在Instructions部分里,提供有ISO的下载地址及CRC和SHA-1值。

我这里提供放一个下载得较多的Visual Studio 2010 Ultimate 试用版的传送门,其余版本请至原网页下载。
下载 [下载没有找到]
-EOF-

HTTP Referer

今天同事遇到了HTTP Referer获取不到的情况,后来一查才发现原来是从HTTPS跳转到HTTP出现的问题。HTTP Referer被广泛运用于来源统计,图片防盗链等。一般情况有发生HTTP Referer为空的原因可能是:
1.直接在浏览器中输入目标URL;
2.页面从HTTPS跳转到HTTP(RFC-2616)
另外Firefox中有相应参数可以设置是否发送HTTP Referer。

network.http.sendRefererHeader (default=2)
设置Referer的发送方式,0为完全不发送,1为只在点击链接时发送,在访问页面中的图像什么的时候不发送,2为始终发送。

network.http.sendSecureXSiteReferrer (default=true)
设置从一个Https访问到另外Https页面的时候是否发送Referer,true为发送,false为不发送。

-EOF-