Game1.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Content;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. using System.Globalization;
  6. namespace LocalizationSample
  7. {
  8. /// <summary>
  9. /// This is the main type for your game.
  10. /// </summary>
  11. public class LocalizationGame : Game
  12. {
  13. #region Fields
  14. GraphicsDeviceManager graphics;
  15. SpriteBatch spriteBatch;
  16. SpriteFont MyFont;
  17. SpriteFont LocalizedFont;
  18. SpriteFont WPFFont;
  19. Texture2D currentFlag;
  20. #endregion
  21. #region Initialization
  22. public LocalizationGame()
  23. {
  24. Content.RootDirectory = "Content";
  25. graphics = new GraphicsDeviceManager(this);
  26. // Tell the resource manager what language to use when loading strings.
  27. Strings.Culture = CultureInfo.CurrentCulture;
  28. }
  29. /// <summary>
  30. /// Load your graphics content.
  31. /// </summary>
  32. protected override void LoadContent()
  33. {
  34. spriteBatch = new SpriteBatch(GraphicsDevice);
  35. MyFont = Content.Load<SpriteFont>("BasicSpriteFont");
  36. LocalizedFont = Content.Load<SpriteFont>("LocalizedFont");
  37. WPFFont = Content.Load<SpriteFont>("WPFFont");
  38. currentFlag = LoadLocalizedAsset<Texture2D>("Flag");
  39. }
  40. /// <summary>
  41. /// Helper for loading a .xnb asset which can have multiple localized
  42. /// versions for different countries. This allows you localize data such
  43. /// as textures, models, and sound effects.
  44. ///
  45. /// This uses a simple naming convention. If you have a default asset named
  46. /// "Foo", you can provide a specialized French version by calling it
  47. /// "Foo.fr", and a Japanese version called "Foo.ja". You can specialize even
  48. /// further by country as well as language, so if you wanted different assets
  49. /// for the United States vs. United Kingdom, you would add "Foo.en-US" and
  50. /// "Foo.en-GB".
  51. ///
  52. /// This function looks first for the most specialized version of the asset,
  53. /// which includes both language and country. If that does not exist, it looks
  54. /// for a version that only specifies the language. If that still does not
  55. /// exist, it falls back to the original non-localized asset name.
  56. /// </summary>
  57. T LoadLocalizedAsset<T>(string assetName)
  58. {
  59. string[] cultureNames =
  60. {
  61. CultureInfo.CurrentCulture.Name, // eg. "en-US"
  62. CultureInfo.CurrentCulture.TwoLetterISOLanguageName // eg. "en"
  63. };
  64. // Look first for a specialized language-country version of the asset,
  65. // then if that fails, loop back around to see if we can find one that
  66. // specifies just the language without the country part.
  67. foreach (string cultureName in cultureNames)
  68. {
  69. string localizedAssetName = assetName + '.' + cultureName;
  70. try
  71. {
  72. return Content.Load<T>(localizedAssetName);
  73. }
  74. catch (ContentLoadException) { }
  75. }
  76. // If we didn't find any localized asset, fall back to the default name.
  77. return Content.Load<T>(assetName);
  78. }
  79. #endregion
  80. #region Update and Draw
  81. /// <summary>
  82. /// Allows the game to run logic.
  83. /// </summary>
  84. protected override void Update(GameTime gameTime)
  85. {
  86. HandleInput();
  87. base.Update(gameTime);
  88. }
  89. /// <summary>
  90. /// This is called when the game should draw itself.
  91. /// </summary>
  92. protected override void Draw(GameTime gameTime)
  93. {
  94. string string1 = Strings.Welcome;
  95. string string2 = string.Format(Strings.CurrentLocale,
  96. CultureInfo.CurrentCulture.EnglishName,
  97. CultureInfo.CurrentCulture);
  98. string string3 = Strings.HowToChange;
  99. GraphicsDevice.Clear(Color.CornflowerBlue);
  100. spriteBatch.Begin();
  101. spriteBatch.DrawString(MyFont, "Welcome to the localization sample!",Vector2.One,Color.White);
  102. spriteBatch.DrawString(LocalizedFont, "Default font Drawing", new Vector2(100, 70), Color.White);
  103. spriteBatch.DrawString(LocalizedFont, string1, new Vector2(100, 100), Color.White);
  104. spriteBatch.DrawString(LocalizedFont, string2, new Vector2(100, 130), Color.White);
  105. spriteBatch.DrawString(LocalizedFont, string3, new Vector2(100, 160), Color.White);
  106. spriteBatch.Draw(currentFlag, new Vector2(100, 210), Color.White);
  107. spriteBatch.DrawString(WPFFont, "WPF font Drawing", new Vector2(100, 330), Color.White);
  108. spriteBatch.DrawString(WPFFont, string1, new Vector2(100, 360), Color.White);
  109. spriteBatch.DrawString(WPFFont, string2, new Vector2(100, 390), Color.White);
  110. spriteBatch.DrawString(WPFFont, string3, new Vector2(100, 420), Color.White);
  111. spriteBatch.End();
  112. base.Draw(gameTime);
  113. }
  114. #endregion
  115. #region Handle Input
  116. /// <summary>
  117. /// Handles input for quitting the game.
  118. /// </summary>
  119. private void HandleInput()
  120. {
  121. KeyboardState currentKeyboardState = Keyboard.GetState();
  122. GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
  123. // Check for exit.
  124. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  125. currentGamePadState.IsButtonDown(Buttons.Back))
  126. {
  127. Exit();
  128. }
  129. }
  130. #endregion
  131. }
  132. }