{"id":1823,"date":"2009-11-01T22:44:20","date_gmt":"2009-11-01T13:44:20","guid":{"rendered":"http:\/\/fukata.org\/?p=1823"},"modified":"2017-08-11T22:59:08","modified_gmt":"2017-08-11T22:59:08","slug":"gae-p-internationalization-1","status":"publish","type":"post","link":"https:\/\/blog.fukata.org\/archives\/1823\/","title":{"rendered":"[GAE\/P]\u56fd\u969b\u5316\u306b\u5bfe\u5fdc\u3057\u3066\u307f\u307e\u3057\u305f\u3000\u301c\u305d\u306e\uff11\u301c"},"content":{"rendered":"
\u73fe\u5728\u3001GAE\/P\u74b0\u5883\u306b\u3066\u4f5c\u6210\u3057\u3066\u3044\u308b\u30a2\u30d7\u30ea\u306e\u56fd\u969b\u5316\u3092\u5bfe\u5fdc\u3057\u3066\u307f\u307e\u3057\u305f\u3002Django-1.0\u3084Kay\u3068\u3044\u3063\u305fFW\u3092\u4f7f\u3063\u3066\u56fd\u969b\u5316\u3082\u5bfe\u5fdc\u3067\u304d\u308b\u3088\u3046\u3067\u3059\u304c\u3001\u4eca\u66f4\u5909\u66f4\u3059\u308b\u306e\u3082\u3060\u308b\u304b\u3063\u305f\u306e\u3067\u3001\u6a19\u6e96\u306e\u74b0\u5883\u3067\u5bfe\u5fdc\u3057\u307e\u3057\u305f\u3002<\/p>\n
\u591a\u5206\u3001\u6b21\u306b\u4f5c\u6210\u3059\u308b\u306a\u3089\u3001\u6a19\u6e96\u306e\u74b0\u5883\u3067\u306f\u306a\u304f\u3001\u4ed6\u306eFW\u3092\u8a66\u3059\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n
\u3068\u307e\u3041\u3001\u81ea\u5206\u306e\u3053\u3068\u306f\u7f6e\u3044\u3068\u3044\u3066\u65e9\u901f\u3001\u56fd\u969b\u5316\u306e\u624b\u9806\u3092\u66f8\u3044\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u307e\u305a\u306f\u3001\u6700\u7d42\u7684\u306a\u69cb\u6210\u3092\u66f8\u304d\u307e\u3059\u3002<\/p>\n
\r\n.\r\n`-- src\r\n |-- app.yaml\r\n |-- conf\r\n | |-- locale\r\n | | |-- en\r\n | | | `-- LC_MESSAGES\r\n | | | |-- django.mo\r\n | | | `-- django.po\r\n | | `-- ja\r\n | | `-- LC_MESSAGES\r\n | | |-- django.mo\r\n | | `-- django.po\r\n | `-- settings.py\r\n `-- org\r\n `-- fukata\r\n `-- mapshare\r\n |-- public\r\n | |-- css\r\n | |-- img\r\n | `-- js\r\n `-- system\r\n |-- handler\r\n | |-- i18NRequestHandler.py\r\n | |-- pc\r\n | | `-- index.py\r\n | `-- webapi\r\n |-- model\r\n |-- template\r\n | `-- pc\r\n `-- utils\r\n `-- cookies.py\r\n<\/pre>\n\u4eca\u56de\u3001\u56fd\u969b\u5316\u306e\u70ba\u306b\u65b0\u898f\u306b\u8ffd\u52a0\u306e\u306f\u4ee5\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u7fa4\u90fd\u306a\u308a\u307e\u3059\u3002<\/p>\n
\u6b21\u306b\u5404\u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9\u306b\u306a\u308a\u307e\u3059\u3002\u8a00\u8a9e\u30d5\u30a1\u30a4\u30eb\uff08.po, .mo\uff09\u306b\u95a2\u3057\u3066\u306f\u7701\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
\u25a0config\/settings.py<\/p>\n
\r\n USE_I18N = True\r\n \r\n # Valid languages\r\n LANGUAGES = (\r\n # 'en', 'zh_TW' should match the directories in conf\/locale\/*\r\n ('en', _('English')),\r\n ('ja', _('Japanese')),\r\n )\r\n \r\n # This is a default language\r\n LANGUAGE_CODE = 'ja'\r\n<\/pre>\n\u25a0org\/fukata\/mapshare\/system\/i18NRequestHandler.py<\/p>\n
\r\n from django.utils import translation\r\n from google.appengine.ext import webapp\r\n from org.fukata.mapshare.system.utils.cookies import Cookies\r\n import os\r\n \r\n os.environ['DJANGO_SETTINGS_MODULE'] = 'conf.settings'\r\n from django.conf import settings\r\n # Force Django to reload settings\r\n settings._target = None\r\n \r\n class I18NRequestHandler(webapp.RequestHandler):\r\n \r\n def initialize(self, request, response):\r\n webapp.RequestHandler.initialize(self, request, response)\r\n \r\n self.request.COOKIES = Cookies(self)\r\n self.request.META = os.environ\r\n self.reset_language()\r\n \r\n def reset_language(self):\r\n \r\n # Decide the language from Cookies\/Headers\r\n language = translation.get_language_from_request(self.request)\r\n translation.activate(language)\r\n self.request.LANGUAGE_CODE = translation.get_language()\r\n \r\n # Set headers in response\r\n self.response.headers['Content-Language'] = translation.get_language()\r\n # translation.deactivate()\r\n<\/pre>\n\u25a0org\/fukata\/mapshare\/system\/utils\/cookies.py<\/p>\n
\r\n import UserDict\r\n from Cookie import BaseCookie\r\n class Cookies(UserDict.DictMixin):\r\n def __init__(self, handler, **policy):\r\n self.response = handler.response\r\n self._in = handler.request.cookies\r\n self.policy = policy\r\n if 'secure' not in policy and handler.request.environ.get('HTTPS', '').lower() in ['on', 'true']:\r\n policy['secure'] = True\r\n self._out = {}\r\n def __getitem__(self, key):\r\n if key in self._out:\r\n return self._out[key]\r\n if key in self._in:\r\n return self._in[key]\r\n raise KeyError(key)\r\n def __setitem__(self, key, item):\r\n self._out[key] = item\r\n self.set_cookie(key, item, **self.policy)\r\n def __contains__(self, key):\r\n return key in self._in or key in self._out\r\n def keys(self):\r\n return self._in.keys() + self._out.keys()\r\n def __delitem__(self, key):\r\n if key in self._out:\r\n del self._out[key]\r\n self.unset_cookie(key)\r\n if key in self._in:\r\n del self._in[key]\r\n p = {}\r\n if 'path' in self.policy: p['path'] = self.policy['path']\r\n if 'domain' in self.policy: p['domain'] = self.policy['domain']\r\n self.delete_cookie(key, **p)\r\n #begin WebOb functions\r\n def set_cookie(self, key, value='', max_age=None,\r\n path='\/', domain=None, secure=None, httponly=False,\r\n version=None, comment=None):\r\n \"\"\"\r\n Set (add) a cookie for the response\r\n \"\"\"\r\n cookies = BaseCookie()\r\n cookies[key] = value\r\n for var_name, var_value in [\r\n ('max-age', max_age),\r\n ('path', path),\r\n ('domain', domain),\r\n ('secure', secure),\r\n ('HttpOnly', httponly),\r\n ('version', version),\r\n ('comment', comment),\r\n ]:\r\n if var_value is not None and var_value is not False:\r\n cookies[key][var_name] = str(var_value)\r\n if max_age is not None:\r\n cookies[key]['expires'] = max_age\r\n header_value = cookies[key].output(header='').lstrip()\r\n self.response.headers._headers.append(('Set-Cookie', header_value))\r\n def delete_cookie(self, key, path='\/', domain=None):\r\n \"\"\"\r\n Delete a cookie from the client. Note that path and domain must match\r\n how the cookie was originally set.\r\n This sets the cookie to the empty string, and max_age=0 so\r\n that it should expire immediately.\r\n \"\"\"\r\n self.set_cookie(key, '', path=path, domain=domain,\r\n max_age=0)\r\n def unset_cookie(self, key):\r\n \"\"\"\r\n Unset a cookie with the given name (remove it from the\r\n response). If there are multiple cookies (e.g., two cookies\r\n with the same name and different paths or domains), all such\r\n cookies will be deleted.\r\n \"\"\"\r\n existing = self.response.headers.get_all('Set-Cookie')\r\n if not existing:\r\n raise KeyError(\r\n \"No cookies at all have been set\")\r\n del self.response.headers['Set-Cookie']\r\n found = False\r\n for header in existing:\r\n cookies = BaseCookie()\r\n cookies.load(header)\r\n if key in cookies:\r\n found = True\r\n del cookies[key]\r\n header = cookies.output(header='').lstrip()\r\n if header:\r\n self.response.headers.add('Set-Cookie', header)\r\n if not found:\r\n raise KeyError(\r\n \"No cookie has been set with the name %r\" % key)\r\n<\/pre>\n\u3068\u306a\u308a\u307e\u3059\u3002\u3067\u3001\u901a\u5e38\u306e\u30cf\u30f3\u30c9\u30e9\u306b\u5bfe\u3057\u3066\u300cwebapp.RequestHandler\u300d\u3067\u306f\u306a\u304f\u3001\u300cI18NRequestHandler\u300d\u3092\u7d99\u627f\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002\u3053\u308c\u3067\u3001\u57fa\u672c\u7684\u306b\u306f\u5927\u4e08\u592b\u3067\u3059\u3002\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u5185\u3067\u3001\u56fd\u969b\u5316\u306e\u30bf\u30b0\u3092\u4f7f\u7528\u3059\u308b\u306b\u306f\u3001\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u5148\u982d\u306a\u3069\u3067\u3001{% load i18n %}\u3068\u8a18\u8ff0\u3057\u307e\u3059\u3002\u3053\u308c\u3067\u3001\u56fd\u969b\u5316\u7528\u306e\u30bf\u30b0\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
\u307e\u305f\u3001.po\u30d5\u30a1\u30a4\u30eb\u306b\u95a2\u3057\u3066\u3067\u3059\u304c\u3001sdk\u306b\u30b9\u30af\u30ea\u30d7\u30c8\u304c\u4ed8\u968f\u3057\u3066\u3044\u308b\u3088\u3046\u3067\u3059\u304c\u3001\u81ea\u5206\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b.mo\u30d5\u30a1\u30a4\u30eb\u3092\u751f\u6210\u3057\u3066\u3044\u307e\u3059\u3002\u307e\u3041\u3001\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u4f7f\u3063\u305f\u65b9\u304c\u4fbf\u5229\u3060\u3068\u601d\u3044\u307e\u3059^^;<\/p>\n
\r\nmsgfmt django.po \r\nmv messages.mo django.mo\r\n<\/pre>\n\u4eca\u56de\u4f7f\u7528\u3057\u305f\u3001\u30b3\u30fc\u30c9\u306b\u95a2\u3057\u3066\u306f\u3001\u53c2\u8003\u30b5\u30a4\u30c8\u306e\u65b9\u306b\u307b\u307c\u3059\u3079\u3066\u3042\u308a\u307e\u3059\u3002\u3068\u3044\u3046\u304b\u3001\u63b2\u8f09\u3055\u308c\u3066\u3044\u308b\u30b3\u30fc\u30c9\u305d\u306e\u307e\u307e\u3067\u52d5\u304d\u307e\u3057\u305f\u3002<\/p>\n
\u25a0\u53c2\u8003\u30b5\u30a4\u30c8<\/p>\n
\u73fe\u5728\u3001GAE\/P\u74b0\u5883\u306b\u3066\u4f5c\u6210\u3057\u3066\u3044\u308b\u30a2\u30d7\u30ea\u306e\u56fd\u969b\u5316\u3092\u5bfe\u5fdc\u3057\u3066\u307f\u307e\u3057\u305f\u3002Django-1.0\u3084Kay\u3068\u3044\u3063\u305fFW\u3092\u4f7f\u3063\u3066\u56fd\u969b\u5316\u3082\u5bfe\u5fdc\u3067\u304d\u308b\u3088\u3046\u3067\u3059\u304c\u3001\u4eca\u66f4\u5909\u66f4\u3059\u308b\u306e\u3082\u3060\u308b\u304b\u3063\u305f\u306e\u3067\u3001\u6a19\u6e96\u306e\u74b0\u5883\u3067\u5bfe\u5fdc\u3057\u307e … \u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":9223372036854775807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1980],"tags":[403,404,1086],"_links":{"self":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/1823"}],"collection":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/comments?post=1823"}],"version-history":[{"count":0,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/1823\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media\/9223372036854775807"}],"wp:attachment":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media?parent=1823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/categories?post=1823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/tags?post=1823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}