2012-03-27 12 views
2

我做在Django幾個徽章類,每個都包含某種描述在一個字符串變量:Django的定位有不願透露姓名的字符串參數

"You get this badge because you've runned %d meters in %d minutes" 
"You get this badge because you've killed %d monsters of the type %s" 

等等之類還有一個功能get_description(badge_level_requirements),所以在模板將與列表一起叫組裝字符串爲特定用戶:

class RunnerBadge(Badge): 
    des=ugettext_lazy("You get this badge because you've runned %d meters in %d minutes") 
    def get_description(cls,badge_level_requirements): 
     return cls.des%badge_level_requirements 

而且我已經存儲在數據庫中的需求列表不帶任何參數的名稱已:(如示例所示,不同的班級有所不同t個數值來填充字符串,這些值也意味着不同的東西。所以我不能真正說出這些論點。

但是,如果我想將這些字符串國際化,將會出現錯誤:'msgid' format string with unnamed arguments cannot be properly localized而且無法爲此問題生成語言文件。

有沒有辦法繞過這個錯誤?

更新

我碰到過這種方法繞過錯誤,而不更改數據庫。 在數據庫中,水平要求都存儲在文本字段在字典的格式:

#Requirment of Runner's badge 
"{'gold':(100,10),'silver':(50,5),'bronze':(25,2)}" 

,並在類的定義,添加碼的手動參數名稱爲「ARG_0」,「ARG_1」 ......以描述。在用於填充描述字符串之前,get_description方法被更改爲預處理數據。

class RunnersBadge(Badge): 
    requirements=#get the previous dict from database 
    description="You get this badge because you've runned %(arg_0)d meters in %(arg_1)d minutes" 

    @classmethod 
    def get_description(cls,level): 
     ''' 
     This is actually a method of parent class Badge 
     level is either 'gold','silver' or 'bronze' 
     ''' 
     dic={} 
     try: 
      for (num,val) in enumerate(cls.requirements[level]): 
       dic['arg_'+str(num)]=val 
     except TypeError: 
      dic['arg_0']=cls.requirements[level] 


     return cls.description%dic 

該方法保留大部分當前結構(邏輯和數據庫)。翻譯者只需要照顧「放置」這個詞。

+1

'runned'不是一個字。你應該用'ran'替換它。 – 2012-03-27 10:27:00

+0

哈哈,這就是爲什麼我試圖繞過限制的一個跡象:) – 2012-03-27 13:00:40

回答

5
  1. 像代碼,variable names should be meaningful within their context, 'meter_count' 和 'MINUTE_COUNT' 是明確的,相對於 'ARG_0' 和 'ARG_1',這是無意義的

  2. 使用standard translation in python code,不太容易出錯並且由令人驚訝的有用makemessages命令識別

  3. 使用use named-string interpolation(例如,%(day)s)而不是位置插值(例如,%s%d),因爲參數的順序可能因語言而異。即德語和拉丁語翻譯名詞/形容詞的順序,日期顯示不同,取決於語言等等...

  4. 使用ran instead of runned,仔細檢查你的英語翻譯字符串

這的語法有效性:

class RunnersBadge(Badge): 
    requirements=#get the previous dict from database 
    description="You get this badge because you've runned %(arg_0)d meters in %(arg_1)d minutes" 

變爲:

from django.utils.translation import ugettext as _ 

class RunnersBadge(Badge): 
    requirements=#get the previous dict from database 
    description=_("You get this badge because you've ran %(meter_count)d meters in %(minute_count)d minutes") 
+0

謝謝jpic!是的,我仍然有疑問,並且因爲我已經以列表格式將值存儲在數據庫中,所以我正在尋找比更改所有記錄更簡單的解決方案:)。 – 2012-03-27 10:14:40

+0

我很抱歉,但我認爲你將不得不改變你的記錄,甚至你的模型結構。有關詳細信息,請參閱我的更新回答 – jpic 2012-03-27 10:41:51

+0

非常感謝您的詳細解釋!我可以理解爲什麼這裏嚴格限制。實際上,我們在一個緊密合作的團隊中專門爲一種語言工作。使用i18n是爲了處理django的默認字符串,並思考爲什麼不把它應用到其他部分呢? :)現在我遇到了一個處理這種情況的「技術」方法。但是如果真的變得難以管理,我可能不得不改變模型。 – 2012-03-27 12:58:07

1

爲了讓不同的方法翻譯str包括數據庫數據或任何用戶生成的內容,我強烈建議Bablic。他們會跟蹤您網站上顯示的字符串,並通知您新的或未翻譯的文本。他們還可以與基於雲的翻譯提供商集成,以便即使是新提交的內容也能立即翻譯,而無需您跟蹤或更新服務器端代碼或配置。

也許這種方法會適合你更多?

相關問題