12
12
import urllib2
13
13
from mpyq import MPQArchive
14
14
15
- from sc2reader import utils
16
- from sc2reader import log_utils
17
- from sc2reader import readers , data
15
+ from sc2reader import utils , log_utils , readers , data
16
+ from sc2reader .exceptions import SC2ReaderLocalizationError
18
17
from sc2reader .objects import Player , Observer , Team , PlayerSummary , Graph , DepotFile
19
18
from sc2reader .constants import REGIONS , LOCALIZED_RACES , GAME_SPEED_FACTOR , GAME_SPEED_CODES , RACE_CODES , PLAYER_TYPE_CODES , TEAM_COLOR_CODES , GAME_FORMAT_CODES , GAME_TYPE_CODES , DIFFICULTY_CODES
20
19
from sc2reader .utils import Color
@@ -629,17 +628,13 @@ def get_url(cls, gateway, map_hash):
629
628
else :
630
629
return None
631
630
632
- class Localization (Resource ):
631
+ class Localization (Resource , dict ):
633
632
634
633
def __init__ (self , s2ml_file , ** options ):
635
- super (Localization , self ).__init__ (s2ml_file , ** options )
636
- self .mapping = dict ()
634
+ Resource .__init__ (self , s2ml_file , ** options )
637
635
xml = ElementTree .parse (s2ml_file )
638
636
for entry in xml .findall ('e' ):
639
- self .mapping [int (entry .attrib ['id' ])] = entry .text
640
-
641
- def __getitem__ (self , key ):
642
- return self .mapping [key ]
637
+ self [int (entry .attrib ['id' ])] = entry .text
643
638
644
639
class GameSummary (Resource ):
645
640
@@ -672,8 +667,8 @@ class GameSummary(Resource):
672
667
#: Map localization urls
673
668
localization_urls = dict ()
674
669
675
- def __init__ (self , summary_file , filename = None , ** options ):
676
- super (GameSummary , self ).__init__ (summary_file , filename ,** options )
670
+ def __init__ (self , summary_file , filename = None , lang = 'enUS' , ** options ):
671
+ super (GameSummary , self ).__init__ (summary_file , filename , lang = lang , ** options )
677
672
678
673
#: A list of teams
679
674
self .team = dict ()
@@ -793,21 +788,25 @@ def load_translations(self):
793
788
self .lang_sheets = dict ()
794
789
self .translations = dict ()
795
790
for lang , files in self .localization_urls .items ():
796
- if lang != 'enUS' : continue
791
+ if lang != self . opt . lang : continue
797
792
798
793
sheets = list ()
799
794
for depot_file in files :
800
795
sheets .append (self .factory .load_localization (depot_file , ** self .opt ))
801
796
802
797
translation = dict ()
803
798
for uid , (sheet , item ) in self .id_map .items ():
804
- translation [uid ] = sheets [sheet ][item ]
799
+ if sheet < len (sheets ) and item in sheets [sheet ]:
800
+ translation [uid ] = sheets [sheet ][item ]
801
+ else :
802
+ msg = "No {0} translation for sheet {1}, item {2}"
803
+ raise SC2ReaderLocalizationError (msg .format (self .opt .lang ,sheet ,item ))
805
804
806
805
self .lang_sheets [lang ] = sheets
807
806
self .translations [lang ] = translation
808
807
809
808
def load_map_info (self ):
810
- map_strings = self .lang_sheets ['enUS' ][- 1 ]
809
+ map_strings = self .lang_sheets [self . opt . lang ][- 1 ]
811
810
self .map_name = map_strings [1 ]
812
811
self .map_description = map_strings [2 ]
813
812
self .map_tileset = map_strings [3 ]
@@ -865,20 +864,22 @@ def use_property(prop, player=None):
865
864
activated [(prop .id ,player )] = use
866
865
return use
867
866
867
+ translation = self .translations [self .opt .lang ]
868
868
for uid , prop in properties .items ():
869
- name = self .translations ['enUS' ][uid ]
869
+ name = self .translations [self . opt . lang ][uid ]
870
870
if prop .is_lobby :
871
871
if use_property (prop ):
872
872
value = prop .values [settings [uid ]][0 ]
873
- self .settings [name ] = self . translations [ 'enUS' ] [(uid ,value )]
873
+ self .settings [name ] = translation [(uid ,value )]
874
874
else :
875
875
for index , player_setting in enumerate (settings [uid ]):
876
876
if use_property (prop , index ):
877
877
value = prop .values [player_setting ][0 ]
878
- self .player_settings [index ][name ] = self . translations [ 'enUS' ] [(uid , value )]
878
+ self .player_settings [index ][name ] = translation [(uid , value )]
879
879
880
880
def load_player_stats (self ):
881
881
if len (self .parts ) < 4 : return
882
+ translation = self .translations [self .opt .lang ]
882
883
883
884
# Part[3][0][:] and Part[4][0][1] are filled with summary stats
884
885
# for the players in the game. Each stat item is laid out as follows
@@ -891,7 +892,7 @@ def load_player_stats(self):
891
892
stats_items .append (self .parts [4 ][0 ][0 ])
892
893
893
894
for item in stats_items :
894
- stat_name = self . translations [ 'enUS' ] [item [0 ][1 ]]
895
+ stat_name = translation [item [0 ][1 ]]
895
896
for index , value in enumerate (item [1 ]):
896
897
if value :
897
898
self .player_stats [index ][stat_name ] = value [0 ][0 ]
@@ -916,6 +917,7 @@ def load_player_stats(self):
916
917
def load_player_builds (self ):
917
918
# Parse build orders only if it looks like we have build items
918
919
if len (self .parts ) < 5 : return
920
+ translation = self .translations [self .opt .lang ]
919
921
920
922
# All the parts after part 5 appear to be designated for
921
923
# build order entries with a max of 10 per part
@@ -927,8 +929,9 @@ def load_player_builds(self):
927
929
# up to the first 64 successful actions in the game.
928
930
BuildEntry = namedtuple ('BuildEntry' ,['supply' ,'total_supply' ,'time' ,'order' ,'build_index' ])
929
931
for build_item in build_items :
930
- if build_item [0 ][1 ] in self .translations ['enUS' ]:
931
- order_name = self .translations ['enUS' ][build_item [0 ][1 ]]
932
+ translation_key = build_item [0 ][1 ]
933
+ if translation_key in translation :
934
+ order_name = translation [translation_key ]
932
935
for pindex , commands in enumerate (build_item [1 ]):
933
936
for command in commands :
934
937
self .build_orders [pindex ].append (BuildEntry (
@@ -939,7 +942,7 @@ def load_player_builds(self):
939
942
build_index = command [1 ] >> 16
940
943
))
941
944
else :
942
- self .logger .warn ("Unknown item in build order, key = {}" .format (build_item [ 0 ][ 1 ] ))
945
+ self .logger .warn ("Unknown item in build order, key = {}" .format (translation_key ))
943
946
944
947
# Once we've compiled all the build commands we need to make
945
948
# sure they are properly sorted for presentation.
0 commit comments